Post JsonObject使用Volley

时间:2016-03-22 21:18:25

标签: android android-volley

我正在使用HttpPost将数据发布到后端服务器,它适用于以下代码:

     public void postData() {

        String url = Configs.URL_OWS;
        String odmName = Configs.ODM;
        String keymd5 = getKeyMD5();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model", model);
        jsonObject.put("imei", imei1);
        jsonObject.put("imei2", imei2);
        jsonObject.put("build", buildVersion);

        if (CommonUtils.isNetworkConnected(mContext)) {
            // Create a new HttpClient and Post Header
            Handler handler = new Handler(Looper.getMainLooper());

            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, Configs.TIME_OUT);
            HttpConnectionParams.setSoTimeout(myParams, Configs.TIME_OUT);
            HttpClient httpclient = new DefaultHttpClient(myParams);

            try {
                HttpPost httppost = new HttpPost(url);
                httppost.setHeader("Accept", "application/json");
                httppost.setHeader("Content-type", "application/json");
                httppost.addHeader("Authorization", odmName + ":" + keymd5);
                // httppost.addHeader("POST", "/api/ows HTTP/1.1");

                StringEntity se = new StringEntity(jsonObject.toString());
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));

                httppost.setEntity(se);
                HttpResponse response = httpclient.execute(httppost);
                String result = EntityUtils.toString(response.getEntity());
                JSONObject jsonResponse = new JSONObject(result);
                String status = jsonResponse.optString("status");

                if (status.equals(Configs.RESPONSE_OK)) { // response 200, send successfull
                    Log.i(Configs.APP_NAME + " " + TAG, "Send data successful");

                } else {
                    Log.i(Configs.APP_NAME + " " + TAG, "Send data failed:");
                }
            } catch (final ClientProtocolException e) {
                Log.i(Configs.APP_NAME + " " + TAG, "ClientProtocolException " + e.getMessage());
            } catch (final IOException e) {
                Log.i(Configs.APP_NAME + " " + TAG, "IOException " + e.getMessage());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.i(Configs.APP_NAME + " " + TAG, "Network not connected ");
        }
    }

我切换到使用Volley而不是HttpPost,但服务器总是返回错误,Volley方法的代码:

    public void postDataByVolley() {

        String url = Configs.URL_OWS;
        String odmName = Configs.ODM;
        final String keymd5 = getKeyMD5();

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("model", model);
        params.put("imei", imei1);
        params.put("imei2", imei2);
        params.put("build", buildVersion);


        if (CommonUtils.isNetworkConnected(mContext)) {


            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                    Request.Method.POST,
                    url,
                    new JSONObject(params),

                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.i(Configs.APP_NAME + " " + TAG, "Success ");
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.i(Configs.APP_NAME + " " + TAG, "Error: " + error.toString());

                        }
                    }) {

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("Accept", "application/json");
                    headers.put("Content-type", "application/json");
                    headers.put("Authorization", odmName + ":" + keymd5);
                    return headers;
                }
            };


            jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(Configs.TIME_OUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            RequestQueue requestQueue = Volley.newRequestQueue(mContext);
            requestQueue.add(jsonObjectRequest);
        }
    }

我找不到Volley方法代码的错误。我的Volley方法有什么问题吗?

1 个答案:

答案 0 :(得分:0)

很难说你为什么会遇到错误。请检查网址和参数是否正确发送。你得到错误然后在这里粘贴错误日志 请查看以下链接以更好地理解

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/