Im试着做POST Retrofit响应,但我必须从这个响应中获取数据。在栈tace中,我看到请求正常,代码== 200,但响应= 0.但是这个请求必须向我提供数据,我从邮递员服务中检查了这个数据。我如何从我的请求中获取数据?response = 0,通过Retrofit
与片段和结果堆栈跟踪从邮差:从邮差
response = {[email protected]}
body = {[email protected]}
signInResult = null
signInResults = null
errorBody = null
rawResponse = {[email protected]} "Response{protocol=http/1.1, code=200, message=OK, url=https://api.vid.me/auth/create}"
body = {[email protected]}
cacheControl = null
cacheResponse = null
request = {[email protected]} "Request{method=POST, url=https://api.vid.me/auth/create, tag=Request{method=POST, url=https://api.vid.me/auth/create, tag=null}}"
handshake = {[email protected]}
headers = {[email protected]} "Access-Control-Allow-Headers: DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Content-Length,Authorization,AccessToken\nAccess-Control-Allow-Methods: GET, POST, OPTIONS, HEAD\nAccess-Control-Allow-Origin: *\nContent-Type: application/json\nDate: Tue, 26 Apr 2016 11:16:04 GMT\nServer: nginx\nSet-Cookie: rid=bbf4159793d4402caf449f8eeabe0404; expires=Sat, 17-May-2031 01:48:34 GMT; Max-Age=475079550; path=/; domain=vid.me\nSet-Cookie: srid=QYpbrONRvejA7DiwZIYTg-dgf8Ug-JBPIKnJL9u7coLmkK585aPsQyY; expires=Sat, 17-May-2031 01:48:34 GMT; Max-Age=475079550; path=/; domain=vid.me\nSet-Cookie: vmtoken=418bdb0823fd49729a51a6d82baf8521; expires=Wed, 26-Apr-2017 11:16:04 GMT; Max-Age=31536000; path=/; domain=vid.me; secure\nStrict-Transport-Security: max-age=31536000; includeSubdomains\nVary: Accept-Encoding\nX-Request-Time: 87\nX-Vidme-Authorization-Okay: false\nX-Vidme-Server-Id: 9610996701283818874f67e619e7c6ef\ntransfer-encoding: chunked\nConn"
message = "OK"
networkResponse = {[email protected]} "Response{protocol=http/1.1, code=200, message=OK, url=https://api.vid.me/auth/create}"
priorResponse = null
protocol = {[email protected]} "http/1.1"
code = 200
结果:
{
"status": true,
"auth": {
"token": "12f6d4123b9947e79f37a4c415bb4b51",
"expires": "2017-04-26 10:38:05",
"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
}
}
我的片段:
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(username_value,password_value);
call.enqueue(new Callback<SignInResults>() {
@Override
public void onResponse(Call<SignInResults> call, Response<SignInResults> response) {
int i = response.body().signInResult.getUser_id();
Log.d("Response ==>> ", String.valueOf(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;
@SerializedName("user_id")
@Expose
private Integer user_id;
public Integer getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
}
SignInResults:
public class SignInResults {
public SignInResult signInResult;
public List<SignInResult> getSignInResults() {
return signInResults;
}
List<SignInResult> signInResults;
}
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(@Field("username") String username,
@Field("password") String password
);
}
错误(当我尝试在onResponse获得值):
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.vid_me_app.FeedFragment$2.onResponse(FeedFragment.java:69)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at dalvik.system.NativeStart.main(Native Method)
谢谢,但我怎么能,例如,我的应用程序的状态和代码? –
SignInResult包含状态和代码字段。 –