android Volley删除方法,为什么会发送空参数

android Volley删除方法,为什么会发送空参数,第1张

概述我使用 android volley库!我有一些不明白从服务器发送请求与json和DELETE方法的问题.请求成功连接到服务器但服务器将收到的已发送参数为空.但标题请求工作正常!请帮我! public void deletePoint(String id) throws JSONException { dialog.show(); queue = Volley.newRequest 我使用 android volley库!我有一些不明白从服务器发送请求与Json和DELETE方法的问题.请求成功连接到服务器但服务器将收到的已发送参数为空.但标题请求工作正常!请帮我!
public voID deletePoint(String ID) throws JsONException {    dialog.show();    queue = Volley.newRequestQueue(getActivity(),new ExthttpClIEntStack(new SslhttpClIEnt().gethttpClIEnt()));    String urlRequest = getUrl();    JsONObject param = new JsONObject();    param.put("ID",ID);    JsonObjectRequest userRequest = new JsonObjectRequest(Request.Method.DELETE,urlRequest,param,deletePointRequestSuccessListener(),reqErrorListener()){        @OverrIDe        public Map<String,String> getheaders() throws AuthFailureError {            Map<String,String> headers = super.getheaders();            if (headers == null || headers.equals(Collections.emptyMap())) {                headers = new HashMap<String,String>();            }            if (ProgressFragment.this.headers != null) {                headers.keySet().removeAll(ProgressFragment.this.headers.keySet());                headers.putAll(ProgressFragment.this.headers);            }            headers.put("Content-Type","application/Json");            return headers;        }    };    userRequest.setRetryPolicy(new DefaultRetryPolicy(            MY_SOCKET_TIMEOUT_MS,DefaultRetryPolicy.DEFAulT_MAX_RETRIES,DefaultRetryPolicy.DEFAulT_BACKOFF_MulT));    dialog.show();    queue.add(userRequest);}private Response.Listener<JsONObject> deletePointRequestSuccessListener() {    return new Response.Listener<JsONObject>() {        @OverrIDe        public voID onResponse(JsONObject response) {            dialog.hIDe();            Gson gson = new Gson();            Success resp = gson.fromJson(response.toString(),Success.class);            if(resp.isSuccess()){                Toast.makeText(getActivity(),getString(R.string.success),Toast.LENGTH_SHORT).show();                try {                    getGraphData();                } catch (JsONException e) {                    e.printstacktrace();                }            }            dialog.hIDe();        }    };}
解决方法 这是 issue已经解决了

你可以重写HurlStack类

public class HurlStack implements httpStack {             break;         case Method.DELETE:             connection.setRequestMethod("DELETE");             addBodyIfExists(connection,request); // here call addBodyIfExists method             break;         case Method.POST:             connection.setRequestMethod("POST");

例如,使用DELETE方法的请求将很容易作为POST

mQueue = Volley.newRequestQueue(context);StringRequest postRequest = new StringRequest(Request.Method.DELETE,httpUtils.URL_MSG,new Response.Listener<String>()    {        @OverrIDe        public voID onResponse(String response) {            if (mCallBack!=null) {            mCallBack.success(response);            }        }    },new Response.ErrorListener()    {        @OverrIDe        public voID onErrorResponse(VolleyError error) {        if (mCallBack!=null) {            mCallBack.fail(null);        }        }    }) {    @OverrIDe    protected Map<String,String> getParams()    {    return params;    }};mQueue.add(postRequest);

那只能解决androID os 5.0设备的问题
androID os 4.2.2设备上有新问题
它将引发以下异常

java.net.ProtocolException: DELETE does not support writing

重写Volley.newRequestQueue(Context context,httpStack stack)方法可以解决这个问题

public static RequestQueue newRequestQueue(Context context,httpStack stack) {    .    .    .    if (stack == null) {        if (Build.VERSION.SDK_INT >= 9) {            stack = new OkhttpStack();        } else {            // Prior to Gingerbread,httpUrlConnection was unreliable.            // See: http://androID-developers.blogspot.com/2011/09/androIDs-http-clIEnts.HTML            stack = new httpClIEntStack(androidhttpclient.newInstance(userAgent));        }    }    .    .    .    return queue;}

OkhttpStack.java(okhttp-1.6.0.jar)

public class OkhttpStack extends HurlStack {  private final OkhttpClIEnt clIEnt;  public OkhttpStack() {    this(new OkhttpClIEnt());  }  public OkhttpStack(OkhttpClIEnt clIEnt) {    if (clIEnt == null) {      throw new NullPointerException("ClIEnt must not be null.");    }    this.clIEnt = clIEnt;  }  @OverrIDe protected httpURLConnection createConnection(URL url) throws IOException {    return clIEnt.open(url);  }   }

它对我有用,希望能为你效劳

总结

以上是内存溢出为你收集整理的android Volley删除方法,为什么会发送空参数全部内容,希望文章能够帮你解决android Volley删除方法,为什么会发送空参数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1138735.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存