我使用过httpclient代码,我想在android中使用Volley库。怎么用Volley

时间:2017-12-19 07:20:41

标签: android android-volley

1)我使用了HttpClient代码" post"请求。

2)但不推荐使用HttpClient Code,所以我想使用Volley库

3)我尝试使用Volley JasonObjectRequest,但它没有工作。

4)请仔细阅读我的代码,并向我提出建议。

5)如何使用Volley Library

class SaveCode extends AsyncTask<Void, Void, String> {
    String resp = new String();
    JSONArray bstring = new JSONArray(listItems);

    @Override
    protected String doInBackground(Void... arg0) {
        JSONObject jsonobj = new JSONObject();
                   Map<String, Object> map = new HashMap<String, Object>();
        try {
            jsonobj.put("Profile", profileDi);
            jsonobj.put("Barcode", bstring);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(config.URL);
                    StringEntity entity = null;
        try {
            entity = new StringEntity(jsonobj.toString());
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        httppost.setEntity(entity);
        try {
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                resp = EntityUtils.toString(responseEntity);
            }
            if (resp != null)
                try {
                    JSONObject obj = new JSONObject(resp);
                    JSONArray msgArray = obj.getJSONArray("Message");
                    boolean isStatus = obj.getBoolean("status");
                    StringBuilder strBuilder = new StringBuilder(1000);
                    for (int i = 0; i < msgArray.length(); i++) {
                        strBuilder.append(msgArray.getString(i));                             
                strBuilder.append(System.getProperty("line.separator"));
                    }
                    errorMsg = strBuilder.toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resp;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        boolean isStatus = false;
       } }

提前致谢。

1 个答案:

答案 0 :(得分:0)

<强>排球 JsonObjectRequest使用参数的HashMap作为JSON格式

Map<String, String> params = new HashMap();
params.put("ProfileId", "Yourid");
params.put("ProfileName", "yourname');

JSONObject parameters = new JSONObject(params);

JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, parameters, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        //TODO: handle success
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
        //TODO: handle failure
    }
});

Volley.newRequestQueue(this).add(jsonRequest);