我有改造的问题。我对这个图书馆很陌生,所以任何帮助都对我意义重大。我正在尝试从应用程序登录到服务器made based on this tutorial(http://localhost/task_manager/v1/login),并在结果中获取有关用户信息的json。
MainActivity.java
private void requestData() {
String email = "ch@ro.me";
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)
答案 0 :(得分:0)
此行引发NullPoinerException
:
error.getBody().toString()
我相信您回来的RetrofitError
没有Body
,因此当您在getBody()
上致电error
时,您会获得null
。然后在其上调用toString()
会引发NPE
。