我试图从POST请求获取数据以在我的应用程序中构建逻辑。
例如;如果code == 200启动主Activity,则生成错误消息。
但是,当我试图从响应值(参数onResponse方法)获取此数据时,我有NPE,并且在堆栈跟踪中我看到response.body()= null。
在堆栈跟踪中,我也看到请求是正确的,并获得代码== 200,status = OK。 从Postman我发现此请求可以获取数据:
{
"status": true,
"auth": {
"token": "f1071c34857e456b8ceb996de3459ab3",
"expires": "2017-04-26 10:01:21",
"user_id": "11272862"
},
"user": {
"user_id": "11272862",
"username": "prozrostyslav",
"full_url": "https://vid.me/prozrostyslav",
"avatar": null,
"avatar_url": "https://d2ha22j8ys3e0o.cloudfront.net/images/default-avatars/13.png?602-2-2-4-9",
"cover": null,
"cover_url": "https://d2ha22j8ys3e0o.cloudfront.net/images/default-covers/03.jpg?602-2-2-4-9",
"displayname": null,
"follower_count": 0,
"likes_count": "0",
"video_count": 0,
"video_views": "0",
"videos_scores": 0,
"comments_scores": 0,
"bio": null
}
}
但我无法在我的应用中获取它:( API接口:
public interface VideoApi {
@GET("/videos/featured")
Call<Videos> getFeaturedVideo();
@GET("/videos/new")
Call<Videos> getNewVideo();
@Headers("Content-Type:application/x-www-form-urlencoded")
@FormUrlEncoded
@POST("/auth/create")
Call<SignInResults>insertUser(@Header("Authorization") String authorization,@Field("username") String username,
@Field("password") String password
);
}
片段:(尝试获取i值时onResponse方法出错)
public class FeedFragment extends Fragment {
EditText username;
EditText password;
Button btnLogin;
public List<SignInResult> signInResult;
String username_value,password_value;
public static final String ROOT_URL = "https://api.vid.me/";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_feed, container, false);
username = (EditText) rootView.findViewById(R.id.user_name_field);
password = (EditText) rootView.findViewById(R.id.password_field);
btnLogin = (Button) rootView.findViewById(R.id.button_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Authorize();
}
});
return rootView;
}
public void Authorize() {
Retrofit retrofitAdapter = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(ROOT_URL)
.build();
final VideoApi videoApi = retrofitAdapter.create(VideoApi.class);
username_value = username.getText().toString();
password_value = password.getText().toString();
String basicauth = "Basic "+ Base64.encodeToString(String.format("%s:%s",username_value,password_value).getBytes(),Base64.NO_WRAP);
Call<SignInResults> call = videoApi.insertUser(basicauth,username_value,password_value);
call.enqueue(new Callback<SignInResults>() {
@Override
public void onResponse(Call<SignInResults> call, Response<SignInResults> response) {
String i = response.body().signInResult.getUser_id();
Log.d("Response ==>> ", i);
}
@Override
public void onFailure(Call<SignInResults> call, Throwable t) {
}
});
}
}
SignInResult:
public class SignInResult {
@SerializedName("authorization")
@Expose
private String authorization;
@SerializedName("code")
@Expose
private String code;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@SerializedName("username")
@Expose
private String username;
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
@SerializedName("user_id")
@Expose
private String user_id;
}
SignInResults:
public class SignInResults {
public SignInResult signInResult;
public List<SignInResult> getSignInResults() {
return signInResults;
}
List<SignInResult> signInResults;
}
答案 0 :(得分:1)
我可以看到你正在使用改装。让我们添加更多日志来改进响应,请求并查看其中的内容。
如果您通过浏览器客户端查询,浏览器可以添加一些额外的人员 例如crftokens,来自cookies的一些信息,但改造不会那样做。
我的示例改装配置。注意:它适用于1.9版本..但是从来没有一个变化。
mRestAdapter = new RestAdapter.Builder()
.setEndpoint(SERVER_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setConverter(gsonConverter)
.setLog(new RestAdapter.Log() {
@Override
public void log(String message) {
Log.d("Retrofit", message);
}
})