0
我試圖從POST請求中獲取數據以在我的應用中構建邏輯。從POST請求中獲取數據
例如;如果代碼== 200開始主要Activity,則會生成錯誤消息。
但是,當我試圖從響應值(參數onResponse方法)獲取此數據時,我有NPE,並在堆棧跟蹤中看到response.body()= null。
在堆棧跟蹤我也看到請求是正確的,並獲得代碼== 200,狀態=確定。 從郵差我看到,這個要求得到我的數據:
{
"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;
}