【问题标题】:post List<List<NameValuePair>> in makehttprequest JSONParser在 makehttprequest JSONParser 中发布 List<List<NameValuePair>>
【发布时间】:2016-11-23 07:32:33
【问题描述】:

目前使用下面在JSONParser 的makehttprequest 中发布List&lt;NameValuePair&gt;。现在需要通过List&lt;List&lt;NameValuePair&gt;&gt;。我已经得到了下面的代码,需要创建一个新函数来传递List&lt;List&lt;NameValuePair&gt;&gt;。

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;

}

【问题讨论】:

  • 你能放一些代码吗
  • 请检查编辑。我正在使用上面的代码。
  • 伙计,你正在被弃用的海洋中淹死。坚持我的答案。 stackoverflow.com/a/40542098/1562723

标签: java android


【解决方案1】:

使用 DefaultHttpClient, HttpPost, HttpResponse, HttpEntity and NameValuePair 是个坏主意,因为所有这些都已弃用,因此我们不应该使用它。 现在您应该使用 HttpURLConnection 和 StringBuilder, Map 向服务器发送数据:

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;
}

所以如果有像 Object 这样的 Map,你可以拥有下一个 Map

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2014-06-10
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多