【问题标题】:Post JsonObject use VolleyPost JsonObject 使用 Volley
【发布时间】:2016-03-22 21:18:24
【问题描述】:

我正在使用 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 方法有什么问题吗?

【问题讨论】:

  • 发布您的错误日志(如果有)。
  • @I_Droid:日志:BasicNetwork.performRequest:意外响应代码 400
  • 400 表示请求错误。检查您是否使用标题和参数正确请求

标签: android android-volley


【解决方案1】:

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

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

【讨论】:

  • 服务器响应:BasicNetwork.performRequest:意外响应代码 400。
  • 只需在此处从您的标头中删除内容类型 json 并检查它是否有效。400 通常意味着服务器 API 没有得到它所期望的内容
猜你喜欢
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多