4

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?

Georgi
  • 674
  • 7
  • 21

2 Answers2

4

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"));
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • 2
    Thanks! I just had to modify the User's data, since the associative array 'User' contains another associative array named '0'. This is how I did it: `params.add(new BasicNameValuePair("User[0][name]", "user's name")); params.add(new BasicNameValuePair("User[0][surname]", "user's surname"));` – Georgi Sep 08 '14 at 10:43
2

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.

Sash_KP
  • 5,551
  • 2
  • 25
  • 34
  • 2
    Thanks! Together with @Illegeal answer it works perfectly. I just had to modify the User's data, since it is an array in an array. This is how I did it for the user's data: `params.add(new BasicNameValuePair("User[0][name]", "name"));` – Georgi Sep 08 '14 at 10:39
  • Great. Glad it worked for you. Please accept an answer for all your questions in SO if it worked for you. – Sash_KP Sep 08 '14 at 15:45
  • 1
    Yeah sure. I do accept this answer even though I also got some good inspiration from @Illegeal answer :) – Georgi Sep 08 '14 at 17:19