如何使用volley使用授权标头在Web服务中发送JSON字符串请求或发布?

时间:2015-06-11 08:50:08

标签: json android-volley

我有一个以前使用httpclient连接到web服务的代码。它正在使用的字符串实体没有问题。我想用凌空更新它。我尝试了下面的代码,但它给了我意想不到的BasicNetwork.performRequest:意外的响应代码400.

JSONObject jsonObject = null;

    try {
        jsonObject = new JSONObject("{\"LoginDate\":\"\\/Date(" + today.getTime() + "+0800)\\/\",\"MAC\":\"00-00-00-12-12-12\",\"MerchantID\":\"xxxxx\",\"StationName\":\"WKSTN01\",\"UserName\":\"exampleuser\"}");
    }catch (Exception e) {
        e.printStackTrace();
    }

    Log.e("JSONOBEJCT", jsonObject.toString());

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonObject , new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jsonObject) {


            Log.e("Response", jsonObject.toString());
            pDialog.hide();
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            VolleyLog.d("Error: " + error.getMessage());
            pDialog.hide();

        }

    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("Authorization", messageHasValue);
            return headers;
        }
    };

这是我的旧代码,我只是想用凌空更新它,有什么方法可以做到吗?...

StringEntity entity = new StringEntity(object.toString());
            HttpClient client = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(URL);

            entity.setContentType("application/json");
            httpPost.addHeader("Authorization", messageHasValue);

            httpPost.setEntity(entity);

            Log.e("HTTPPOST ", httpPost.toString());

            HttpResponse response = client.execute(httpPost);
            HttpEntity resEntity = response.getEntity();

1 个答案:

答案 0 :(得分:0)

它现在正在工作,我尝试将我的PHP版本从4升级到5并格式化TimeStamp。