Skip to content
Advertisement

Error while sending data with POST request in Android

I always get the same error while sending username and password to server. I need to send data as content type:form-data, because server isn’t responding on any other type. I tested in postman and it’s ok, but when I am trying to send it in code, it’s not working. Can someone help me, thanks!!

  public class Login extends AsyncTask<String, Void, Void> {
        
                String userName;
                String password;
        
                public Login(String user, String pass) {
                    userName = user;
                    password = pass;
                }
        
                protected void onPreExecute() {
                    super.onPreExecute();
        
                }
        
        
         
 

protected Void doInBackground(String... params) {
OkHttpClient client = new OkHttpClient().newBuilder()
            .build();
    MediaType mediaType = MediaType.parse("text/plain");
    RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
            .addFormDataPart("user",userName)
            .addFormDataPart("pass",password)
            .build();
    Request request = new Request.Builder()
            .url("http://www.autotrack.rs/android_juzna_backa/login.php")
            .method("POST", body)
            .build();
        try {
            Response response = client.newCall(request).execute();
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
                try {
                    Call call = client.newCall(request);
                    Response response = call.execute();
                    System.out.println(response.body().string());
    
                }catch (Exception m)
                {
                    Log.d("Mytag", m.getMessage());
                }
                if(!response.isSuccessful())
                {
                    try {
                        throw new IOException("Unexpected: "+response);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘boolean okhttp3.Response.isSuccessful()’ on a null object reference

Advertisement

Answer

This code works perfectly fine for me:

        OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
  .addFormDataPart("user","pera")
  .addFormDataPart("pass","1111")
  .build();
Request request = new Request.Builder()
  .url("http://www.autotrack.rs/android_juzna_backa/login.php")
  .method("POST", body)
  .build();
Response response = client.newCall(request).execute();

In your code I see that you execute the same request twice, maybe this is the issue?

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement