我正在尝试学习Volley库,以便将数据发布到webservices中.我需要实现用户注册表,以下是带有参数和标题的邮递员的图像……
现在的问题是,我正在低于错误
com.androID.volley.ServerError
这是我的凌空发布方法的代码.
public voID postNewComment(){ try { RequestQueue requestQueue = Volley.newRequestQueue(this); String URL = "http://myurl/API/users"; JsONObject JsonBody = new JsONObject(); JsonBody.put("email", "test1@gmail.com"); JsonBody.put("user_type", "C"); JsonBody.put("company_ID", "0"); JsonBody.put("status", "A"); JsonBody.put("password", "123456"); final String requestbody = 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 one rrorResponse(VolleyError error) { error.printstacktrace(); Log.e("VolLEY", error.toString()); } }) { @OverrIDe public String getbodyContentType() { return "application/Json; charset=utf-8"; } @OverrIDe public Map<String, String> getheaders() throws AuthFailureError { final Map<String, String> headers = new HashMap<>(); headers.put("Authorization", "Basic " + "My_auth_key"); headers.put("Content-Type", "application/Json"); return headers; } @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 } return Response.success(responseString, httpheaderParser.parseCacheheaders(response)); } }; requestQueue.add(stringRequest); } catch (JsONException e) { e.printstacktrace(); }}
请告诉我哪里出错了.网址与邮递员的工作正确,你也可以看到我需要设置2个标题.我也尝试了这个使用AsyncTask的Url post方法及其工作方式.现在我需要使用volley库来实现它.善意的建议.谢谢.
这是我的logcat错误:
E/Volley: [81910] BasicNetwork.performRequest: Unexpected response code 405 for “Myurl”
解决方法:
**试试这一个**
private voID senDWorkPostRequest() { try { String URL = ""; JsONObject JsonBody = new JsONObject(); JsonBody.put("email", "abc@abc.com"); JsonBody.put("password", ""); JsonBody.put("user_type", ""); JsonBody.put("company_ID", ""); JsonBody.put("status", ""); JsonObjectRequest JsonOblect = new JsonObjectRequest(Request.Method.POST, URL, JsonBody, new Response.Listener<JsONObject>() { @OverrIDe public voID onResponse(JsONObject response) { Toast.makeText(getApplicationContext(), "Response: " + response.toString(), Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @OverrIDe public voID one rrorResponse(VolleyError error) { onBackpressed(); } }) { @OverrIDe public Map<String, String> getheaders() throws AuthFailureError { final Map<String, String> headers = new HashMap<>(); headers.put("Authorization", "Basic " + "c2FnYXJAa2FydHBheS5jb206cnMwM2UxQUp5RnQzNkQ5NDBxbjNmUDgzNVE3STAyNzI=");//put your token here return headers; } }; VolleyApplication.getInstance().addToRequestQueue(JsonOblect); } catch (JsONException e) { e.printstacktrace(); } // Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show(); }}
总结 以上是内存溢出为你收集整理的如何在带有参数和标题的android Volley库中使用POST请求?全部内容,希望文章能够帮你解决如何在带有参数和标题的android Volley库中使用POST请求?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)