Android HttpPost发送ArrayList

时间:2014-08-11 07:13:50

标签: android http-post

我尝试使用arraylists发送httpPost。我搜索并了解如何发送httpPost。我编写的代码可以发送简单的httpPost,但我不知道如何发送Arraylist

这是我的代码:

public static String SendHttpPost(String Url,String method,HashMap<String,String> args,int id)
{
      HttpResponse response = null;
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(Url);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("method", method));
        nameValuePairs.add(new BasicNameValuePair("args", args.put("objectType", String.valueOf(0))));

        nameValuePairs.add(new BasicNameValuePair("args", args.put("languageISOCode", "eng")));
        nameValuePairs.add(new BasicNameValuePair("id", String.valueOf(id)));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        response= httpclient.execute(httppost);


    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch blockF
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
    return String.valueOf(response);
}

我会像这样发送Arralist

{"objectType" : 0,"languageISOCode" : "eng"}

如果有人知道解决方案,请帮助我。

3 个答案:

答案 0 :(得分:0)

由于您尝试发送命名参数而不仅仅是数组列表(数字到字符串的映射),我建议我使用这种方法:

private static void setHttpParameters(HttpUriRequest request,
        String... alternatingNameValueParams) throws IOException {
    if (request instanceof HttpEntityEnclosingRequest) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for (int i = 0; i < alternatingNameValueParams.length; i += 2) {
            params.add(new BasicNameValuePair(
                    alternatingNameValueParams[i],
                    alternatingNameValueParams[i + 1]));
        }
        ((HttpEntityEnclosingRequest) request)
                .setEntity(new UrlEncodedFormEntity(params));
    } else {
        HttpParams params = new BasicHttpParams();
        for (int i = 0; i < alternatingNameValueParams.length; i += 2) {
            params.setParameter(alternatingNameValueParams[i],
                    alternatingNameValueParams[i + 1]);
        }
        request.setParams(params);
    }
}

现在使用以下内容替换SendHttpPost方法中的所有内容:

HttpResponse response = null;
BasicHttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
DefaultHttpClient httpclient = new DefaultHttpClient();
ClientConnectionManager mgr = httpclient.getConnectionManager();
httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(
    params, mgr.getSchemeRegistry()), params);
HttpPost httppost = new HttpPost(Url);

try {
    // Add your data
    setHttpParameters(httppost, "method", method, "objectType",
        String.valueOf(0), "languageISOCode", "eng", "id", String.valueOf(id));

    // Execute HTTP Post Request
    String response = httpclient.execute(httppost);
    return new BasicResponseHandler().handleResponse(response);
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
} catch (HttpResponseException e) {
    // TODO Auto-generated catch block
}
return null;

答案 1 :(得分:0)

为数据创建bean类,例如:“如果要发送Objecttype,lang_code等,可以创建类似LocaleInformation的类”,然后创建“LocaleInformation”类型的ArrayList。之后,很容易创建LocaleInformation实例并为其分配成员变量值,然后将其添加到ArrayList。

或者

您可以简单地使用Volley库来加快网络连接。很多示例都可供Volley使用。

答案 2 :(得分:0)

你应该尝试使用谷歌的排球。 http://developer.android.com/training/volley/index.html

一切都简单得多。如果您正在尝试发送ArrayList,最好将其作为JSON对象发送,即imo。