1

Currently using below to post List<NameValuePair> in makehttprequest in JSONParser. And now need to pass List<List<NameValuePair>>. I already got the below code and need to create new function to pass List<List<NameValuePair>> in that.

public JSONObject makeHttpRequest(String url, String method,
                                  List<NameValuePair> params) {
try {
        System.setProperty("http.keepAlive", "false");
        // check for request method
        if (method == "POST") {
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(group,""));
            httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } else if (method == "GET") {
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            if (params.size() != 0) {
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

Using DefaultHttpClient, HttpPost, HttpResponse, HttpEntity and NameValuePair is a bad idea because all of this are deprecated so we shouldn't use it. Now you should use HttpURLConnection and StringBuilder, Map to send data to server:

protected JSONArray getResult(URL url, Map<String, Object> urlParams {
    HttpURLConnection conn;
    BufferedReader in;
    String line;
    StringBuilder chaine = new StringBuilder();
    StringBuilder postData = new StringBuilder();
    byte[] postDataBytes = new byte[0];

    // urlParams = Map<String, Object>
    for (Map.Entry<String, Object> param : urlParams.entrySet()) {
        if (postData.length() != 0) postData.append('&');
        try {
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
            postDataBytes = postData.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
    try { 
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));

        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);
        in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        while ((line = in.readLine()) != null) {
            chaine.append(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    JSONArray result = null;
    try {
        result = new JSONArray(chaine.toString());

    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.e("DATABASE_REST Result", result.toString());
    return result;
}

So if there is a Map like Object you can have next Map

qubuss
  • 268
  • 3
  • 12