本文介绍了凌空JsonObjectRequest发布采购信息不灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的Android凌空发出请求。所以我用这个code。我不明白的一件事。我检查我的服务器PARAMS总是空。我认为getParams()方法不工作。我应该怎么做来解决这个问题。
请求队列排队= MyVolley.getRequestQueue();
JsonObjectRequest jsObjRequest =新JsonObjectRequest(Request.Method.POST,SPHERE_URL,空,
新Response.Listener<的JSONObject>(){
@覆盖
公共无效onResponse(JSONObject的响应){
的System.out.println(响应);
hideProgressDialog();
}
},
新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
hideProgressDialog();
}
}){
保护地图<字符串,字符串> getParams()方法抛出AuthFailureError {
地图<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
params.put(ID,1);
params.put(姓名,myname中);
返回PARAMS;
};
};
queue.add(jsObjRequest);
解决方案
尝试使用这种辅助类
进口java.io.UnsupportedEncodingException;
进口的java.util.Map;
进口org.json.JSONException;
进口org.json.JSONObject;
进口com.android.volley.NetworkResponse;
进口com.android.volley.ParseError;
进口com.android.volley.Request;
进口com.android.volley.Response;
进口com.android.volley.Response.ErrorListener;
进口com.android.volley.Response.Listener;
进口com.android.volley.toolbox.HttpHeaderParser;
公共类CustomRequest扩展请求<的JSONObject> {
私人听者LT;的JSONObject>侦听器;
私人地图<字符串,字符串> PARAMS;
公共CustomRequest(字符串URL,地图<字符串,字符串>参数,可以
收听LT;的JSONObject> reponseListener,ErrorListener errorListener){
超(Method.GET,网址,errorListener);
this.listener = reponseListener;
this.params = PARAMS;
}
公共CustomRequest(INT方法,字符串URL,地图<字符串,字符串>参数,可以
收听LT;的JSONObject> reponseListener,ErrorListener errorListener){
超(方法,URL,errorListener);
this.listener = reponseListener;
this.params = PARAMS;
}
保护地图<字符串,字符串> getParams()方法
抛出com.android.volley.AuthFailureError {
返回PARAMS;
};
@覆盖
受保护的响应和LT;的JSONObject> parseNetworkResponse(NetworkResponse响应){
尝试 {
字符串jsonString =新的String(response.data,
HttpHeaderParser.parseCharset(response.headers));
返回Response.success(新的JSONObject(jsonString)
HttpHeaderParser.parseCacheHeaders(响应));
}赶上(UnsupportedEncodingException E){
返回Response.error(新ParseError(e)条);
}赶上(JSONException JE){
返回Response.error(新ParseError(JE));
}
}
@覆盖
保护无效deliverResponse(JSONObject的响应){
// TODO自动生成方法存根
listener.onResponse(响应);
}
}
在活动/片段确实使用此
请求队列请求队列= Volley.newRequestQueue(getActivity());
CustomRequest jsObjRequest =新CustomRequest(Method.POST,URL,参数,可以this.createRequestSuccessListener(),this.createRequestErrorListener());
requestQueue.add(jsObjRequest);
I am using android Volley for making a request. So I use this code. I don't understand one thing. I check in my server that params is always null. I consider that getParams() not working. What should I do to solve this issue.
RequestQueue queue = MyVolley.getRequestQueue();
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,SPHERE_URL,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
hideProgressDialog();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hideProgressDialog();
}
}) {
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("id","1");
params.put("name", "myname");
return params;
};
};
queue.add(jsObjRequest);
解决方案
try to use this helper class
import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
public class CustomRequest extends Request<JSONObject> {
private Listener<JSONObject> listener;
private Map<String, String> params;
public CustomRequest(String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public CustomRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return params;
};
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONObject response) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
}
In activity/fragment do use this
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener());
requestQueue.add(jsObjRequest);
这篇关于凌空JsonObjectRequest发布采购信息不灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!