本文介绍了收到Android上的POST请求空响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用抽射执行android上休息的请求。当我做一个登录尝试失败给予回应,而是返回一个空的响应。
在服务器端运行正常,我已经收到在Android客户端上的空响应。
这是code我写的:
的HashMap<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
params.put(电子邮件,[email protected]);
params.put(密码,123456); JsonObjectRequest jsonObjReq =新JsonObjectRequest(Request.Method.POST,
urlJsonObj,新的JSONObject(PARAMS),新Response.Listener<&JSONObject的GT;(){ @覆盖
公共无效onResponse(JSONObject的响应){
Log.d(TAG,response.toString());
Toast.makeText(getApplicationContext(),
response.toString(),
Toast.LENGTH_LONG).show();
hidepDialog();
}
},新Response.ErrorListener(){ @覆盖
公共无效onErrorResponse(VolleyError错误){
VolleyLog.d(TAG,错误:+ error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(),Toast.LENGTH_SHORT).show();
//隐藏进度对话框
hidepDialog();
}
}); //添加请求,请求队列
。AppController.getInstance()addToRequestQueue(jsonObjReq);
解决方案
显示如下这个问题的正确答案。在这个版本的要求,邮政参数在现有getParams重写()凌空方法。我做的错误是没有覆盖的方法。
字符串的URL =http://httpbin.org/post;StringRequest postRequest =新StringRequest(Request.Method.POST,网址,
新Response.Listener<串GT;(){
@覆盖
公共无效onResponse(字符串响应){
尝试{
JSONObject的jsonResponse =新的JSONObject(响应).getJSONObject(形式);
字符串网站= jsonResponse.getString(简称网站),
网络= jsonResponse.getString(网络);
的System.out.println(网站:+网站+\\ n网络:+网络);
}赶上(JSONException E){
e.printStackTrace();
}
}
},
新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
error.printStackTrace();
}
}
){
@覆盖
保护地图<字符串,字符串> getParams()方法
{
地图<字符串,字符串> PARAMS =新的HashMap<>();
//将POST参数:
params.put(网站,code);
params.put(网络,tutsplus);
返回PARAMS;
}
};
Volley.newRequestQueue(本)。新增(postRequest);
I'm using Volley to perform rest requests on android. When I make a login attempt it fails to give the response and instead returns an empty response.
The server side works fine, I have received the empty response on Android client side.This is the code I wrote:
HashMap<String, String> params = new HashMap<String, String>();
params.put("email", "[email protected]");
params.put("password", "123456");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
urlJsonObj, new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
Toast.makeText(getApplicationContext(),
response.toString(),
Toast.LENGTH_LONG).show();
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
解决方案
The correct Answer for this question is shown below. In this version of the request, the Post parameters are overriden in the existing getParams() method of Volley. The mistake I did was to not override that method.
String url = "http://httpbin.org/post";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response).getJSONObject("form");
String site = jsonResponse.getString("site"),
network = jsonResponse.getString("network");
System.out.println("Site: "+site+"\nNetwork: "+network);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
// the POST parameters:
params.put("site", "code");
params.put("network", "tutsplus");
return params;
}
};
Volley.newRequestQueue(this).add(postRequest);
这篇关于收到Android上的POST请求空响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!