Android Volley onResponse 获取数据
Android Volley onResponse get data
在字符串 post 登录请求中。在 onResponse 中,我返回的只是状态代码 200。我也想获得返回的数据。我不确定从现在开始该去哪里?关于如何获取返回数据的任何想法,而不仅仅是状态代码?
public void requestWithSomeHttpHeaders() {
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://......";
JSONObject jsonBody = new JSONObject();
jsonBody.put("username", "yourusername");
jsonBody.put("password", "yourpassword");
final String mRequestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
System.out.println(responseString);
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
最好定义一个方法。
private void doLogin() {
// TODO: startActivity?
}
然后调用那个
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
doLogin();
}
如果你想要服务器的数据,那就是String response
。检查你的 Logcat。
如评论中所述,使用 StringRequest
而不实施 getBody
、parseNetworkResponse
和 getBodyContentType
可能效果更好。
如果你想解析 JSON 添加这样的方法
public Profile getUserProfile(String response){
Profile profile = null;
try {
profile = new Profile();
JSONObject jsonObject = new JSONObject(response);
profile.setId(jsonObject.getInt(context.getString(R.string.profile_id)));
profile.setLastName(jsonObject.getString(context.getString(R.string.profile_lastName)));
profile.setName(jsonObject.getString(context.getString(R.string.profile_name)));
profile.setEmail(jsonObject.getString(context.getString(R.string.profile_email)));
} catch (JSONException | NullPointerException e) {
e.printStackTrace();
return null;
}
return profile;
}
在你的 onResponse 方法中
@Override
public void onResponse(String response) {
//If you have son object
Profile profile = getUserProfile(response)
}
在字符串 post 登录请求中。在 onResponse 中,我返回的只是状态代码 200。我也想获得返回的数据。我不确定从现在开始该去哪里?关于如何获取返回数据的任何想法,而不仅仅是状态代码?
public void requestWithSomeHttpHeaders() {
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://......";
JSONObject jsonBody = new JSONObject();
jsonBody.put("username", "yourusername");
jsonBody.put("password", "yourpassword");
final String mRequestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
System.out.println(responseString);
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
最好定义一个方法。
private void doLogin() {
// TODO: startActivity?
}
然后调用那个
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
doLogin();
}
如果你想要服务器的数据,那就是String response
。检查你的 Logcat。
如评论中所述,使用 StringRequest
而不实施 getBody
、parseNetworkResponse
和 getBodyContentType
可能效果更好。
如果你想解析 JSON 添加这样的方法
public Profile getUserProfile(String response){
Profile profile = null;
try {
profile = new Profile();
JSONObject jsonObject = new JSONObject(response);
profile.setId(jsonObject.getInt(context.getString(R.string.profile_id)));
profile.setLastName(jsonObject.getString(context.getString(R.string.profile_lastName)));
profile.setName(jsonObject.getString(context.getString(R.string.profile_name)));
profile.setEmail(jsonObject.getString(context.getString(R.string.profile_email)));
} catch (JSONException | NullPointerException e) {
e.printStackTrace();
return null;
}
return profile;
}
在你的 onResponse 方法中
@Override
public void onResponse(String response) {
//If you have son object
Profile profile = getUserProfile(response)
}