0

I try to send arraylists with httpPost. I searched and learn how to can send httpPost. I wrote code with can send simple httpPost, but I do not know how I can send Arraylist.

This is my code:

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

I would to send like this Arralist:

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

If anyone knows solution, please help me.

BekaKK
  • 2,173
  • 6
  • 42
  • 80

3 Answers3

0

Since you're trying to send named arguments and not just an array list (mapping of numbers to strings), I would suggest this method I'm using:

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

Now replace everything inside your SendHttpPost method with the following:

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;
0101100101
  • 5,786
  • 6
  • 31
  • 55
0

Create bean class for data what you want to send for example : " if you want to send Objecttype , lang_code etc . you can create class like LocaleInformation" and then create ArrayList of "LocaleInformation" type. After that it would be easy to create LocaleInformation instance and assign member variables values to it and then add it to ArrayList.

Or

You can simply use Volley library for faster networking.Many examples are available out there for Volley.

Deep Shah
  • 1,334
  • 3
  • 20
  • 41
0

You should try using Google's Volley. http://developer.android.com/training/volley/index.html

Everything is much simpler. If you're trying to send an ArrayList, best to send it as JSON object, imo.

nightfixed
  • 871
  • 1
  • 12
  • 24