java Retrofit + POST 方式 + www-form-urlencoded
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29738230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrofit + POST method + www-form-urlencoded
提问by 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.
我有改造问题。我对这个图书馆很陌生,所以任何帮助对我来说都很重要。我正在尝试从应用程序登录到基于本教程制作的服务器(http://localhost/task_manager/v1/login),并在结果中获取包含用户信息的 json。
MainActivity.java
主活动.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
用户接口.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
用户.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.failure(MainActivity.java:87)
04-20 01:38:20.540: E/AndroidRuntime(671): at retrofit.CallbackRunnable.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)
采纳答案by Emmanuel
The NullPoinerException
is being thrown by this line:
在NullPoinerException
被抛出的这一行:
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
.
我相信RetrofitError
你回来的时候没有Body
,所以当你打电话getBody()
时error
你得到null
。然后调用toString()
它会提高NPE
.