Georgi
Georgi

Reputation: 674

Http post in android with nested associative array

I am trying to send an http post request to a PHP service. Here is an example of how the input may look with some test data enter image description here

I know that the Java alternative to the PHP associative arrays are HashMaps, but I wonder can this be done with NameValuePairs? What is the best way to format this input and call the PHP service via post request?

Upvotes: 4

Views: 2704

Answers (2)

Illegal Argument
Illegal Argument

Reputation: 10358

Extending @Sash_KP's answer, you can post the nameValuePairs like this too:

params.add(new BasicNameValuePair("Company[name]", "My company"));
params.add(new BasicNameValuePair("User[name]", "My Name"));

Upvotes: 4

Sash_KP
Sash_KP

Reputation: 5591

Yes this can be done with NameValuePair.You can have something like

List<NameValuePair> params;
//and when making `HttpPost` you can do 
HttpPost httpPost = new HttpPost("Yoururl");
httpPost.setEntity(new UrlEncodedFormEntity(params));

//and while building parameters you can do somethin like this 
params.add(new BasicNameValuePair("name", "firemanavan"));
params.add(new BasicNameValuePair("cvr", "1245678"));
....

Here's a neat and nice parsing method which you can use.

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
    InputStream is = null;
    String json = "";
    JSONObject jObj = null;

    // Making HTTP request
    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        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();
        Log.e("JSON", json);
    } 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 jObj;

} 

And you can simply use it something like

getJSONFromUrl("YourUrl", params);

Now this is just a basic idea of how you can achieve this using NameValuePair.You will have to need some more workaround to implement exactly as you want, but this should provide you the basic idea.Hope this helps.

Upvotes: 2

Related Questions