Retrofit + POST method + www-form-urlencoded

Kamajabu

I have a problem with retrofit. I'm pretty new to this library so any help would mean a lot to me. I'm trying to login from app to server made based on this tutorial (http://localhost/task_manager/v1/login), and get json with info about user in result.

MainActivity.java

private void requestData() {

    String email = "[email protected]";
    String password = "chrome11";
    //retrofit tworzenie polecenia
    RestAdapter adapter = new RestAdapter.Builder()
      .setEndpoint(ENDPOINT)
      .build();

    //tworzenie api klasy flowers
    UsersAPI api = adapter.create(UsersAPI.class);


    api.login(email, password, new Callback < User > () {@
      Override
      public void failure(final RetrofitError error) {
        android.util.Log.i("example", "Error, body: " + error.getBody().toString());

      }@
      Override
      public void success(User user, Response response) {
        // Do something with the User object returned
        //Log.d("hello", response.toString());
      }
    });

UsersApi.java

public interface UsersAPI {

  //    @Headers({
  //        "content-type:application/x-www-form-urlencoded"
  //    })
  @FormUrlEncoded
  @POST("/login")
  public void login(@Field("email") String email, @Field("password") String password,
    Callback < User > callback);
}

User.java

public class User {

  private String error;
  private String name;
  private String email;
  private String apiKey;
  private String createdAt;

  public String getError() {
    return error;
  }
  public void setError(String error) {
    this.error = error;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public String getApiKey() {
    return apiKey;
  }
  public void setApiKey(String apiKey) {
    this.apiKey = apiKey;
  }
  public String getCreatedAt() {
    return createdAt;
  }
  public void setCreatedAt(String createdAt) {
    this.createdAt = createdAt;
  }


}
04-20 01:37:46.358: D/gralloc_goldfish(671): Emulator without GPU emulation detected.
04-20 01:37:55.469: D/dalvikvm(671): GC_CONCURRENT freed 200K, 3% free 10978K/11271K, paused 26ms+15ms, total 87ms
04-20 01:38:20.518: D/AndroidRuntime(671): Shutting down VM
04-20 01:38:20.518: W/dalvikvm(671): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-20 01:38:20.540: E/AndroidRuntime(671): FATAL EXCEPTION: main
04-20 01:38:20.540: E/AndroidRuntime(671): java.lang.NullPointerException
04-20 01:38:20.540: E/AndroidRuntime(671):  at retrofit.RetrofitError.getBody(RetrofitError.java:80)
04-20 01:38:20.540: E/AndroidRuntime(671):  at com.hanselandpetal.catalog.MainActivity$1.failure(MainActivity.java:87)
04-20 01:38:20.540: E/AndroidRuntime(671):  at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
04-20 01:38:20.540: E/AndroidRuntime(671):  at android.os.Handler.handleCallback(Handler.java:615)
04-20 01:38:20.540: E/AndroidRuntime(671):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 01:38:20.540: E/AndroidRuntime(671):  at android.os.Looper.loop(Looper.java:137)
04-20 01:38:20.540: E/AndroidRuntime(671):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-20 01:38:20.540: E/AndroidRuntime(671):  at java.lang.reflect.Method.invokeNative(Native Method)
04-20 01:38:20.540: E/AndroidRuntime(671):  at java.lang.reflect.Method.invoke(Method.java:511)
04-20 01:38:20.540: E/AndroidRuntime(671):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-20 01:38:20.540: E/AndroidRuntime(671):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-20 01:38:20.540: E/AndroidRuntime(671):  at dalvik.system.NativeStart.main(Native Method)
Emmanuel

The NullPoinerException is being thrown by this line:

error.getBody().toString()

I believe the RetrofitError you are getting back does not have a Body, so when you call getBody() on error you get null. Then calling toString() on it will raise the NPE.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to POST x-www-form-urlencoded in retrofit

How to send post request with content-type x-www-form-urlencoded android retrofit

How to post (x-www-form-urlencoded) Json Data using Retrofit?

why can't Volley String request send Body parameters with POST method in x-www-form-urlencoded?

PowerShell - Slack API - Room History - Post method with x-www-form-urlencoded parameters

Android Retrofit: content type as application/x-www-form-urlencoded

how to post body x-www-form-urlencoded using webclient?

Jersey client Post Request with x-www-form-urlencoded Fails

Spring - wrong encoding POST request with x-www-form-urlencoded

Post a x-www-form-urlencoded request from React Native

How to send post request with x-www-form-urlencoded body

post application/x-www-form-urlencoded Alamofire

Swift 4 send POST request as x-www-form-urlencoded

RestSharp post request - Body with x-www-form-urlencoded values

How to POST content as application/x-www-form-urlencoded

How to send x-www-form-urlencoded in a post request in webclient?

Axios post form urlencoded requests application/x-www-form-urlencoded

How to convert x-www-form-urlencoded post Message to JSON post Message?

Angularjs $http.post to contact form 7 with content type application/x-www-form-urlencoded

How to do a post request using FORM-DATA or x-www-form-urlencoded with Android?

How to send post request with x-www-form-urlencoded body with loopj-async-http library

how to post data in node.js with content type ='application/x-www-form-urlencoded'

How to escape + in post call content type as application/x-www-form-urlencoded

How to get and parse JSON response from x-www-form-urlencoded POST, RestTemplate (Java)?

Post controller recognizes only parameters sent by "from-data" or "x-www-form-urlencoded" from postman

CORS issue when trying to do JQuery post with json data, but not with plain text or x-www-form-urlencoded

How to do a x-www-form-urlencoded POST login using cypress?

How to force Angular2 to POST using x-www-form-urlencoded

Node.js Axios POST request with application/x-www-form-urlencoded format?