1

I have the following url and data

URL: http://......com/hotel/api/customerapi/customer_register?APIKEY=9ffeb408h93053dc0e59a0c3f8814bcb8df30b69

Input Data Format:

{"data":{"customer_name":"ass","hotel_id":"6","mobile_no":"999966632"}}

Is it possible? If yes how can I pass this data?

What I tried:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("customer_name", "ass"));
nameValuePairs.add(new BasicNameValuePair("hotel_id", "6"));
nameValuePairs.add(new BasicNameValuePair("mobile_no", "999966632"));       

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

But in php side I can access it like only "{"customer_name":"ass","hotel_id":"6","mobile_no":"999966632"}"

1
  • Why exactly did it not help you? Have you tried to implement the code there and if yes, please share the errors you might have gotten. Commented Apr 4, 2017 at 16:39

1 Answer 1

2

You will need to use something like Volley - here

HashMap<String, String> params = new HashMap<String, String>();
    params.put("customer_name", "ass");
    params.put("hotel_id", "6");
    params.put("mobile_no", "999966632");

String YourURL = "http://......com/hotel/api/customerapi/customer_register?APIKEY=9ffeb408h93053dc0e59a0c3f8814bcb8df30b69";

JsonObjectRequest request_json = new JsonObjectRequest(YourURL, new JSONObject(params)),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            //Do what you want on response 
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //If there's an error...
            }
        });

        //Add process to queue to get JSON in background thread
        queue.add(request_json);

EDIT

JsonObject jo = Json.createObjectBuilder()
  .add("data", Json.createArrayBuilder()
    .add(Json.createObjectBuilder()
      .add("etc", "etc")
      .add("etc", "etc")))
  .build();
Sign up to request clarification or add additional context in comments.

2 Comments

where did you mention the {"data":? object
See updated answer. I would then just pass JsonObject instead of new JsonObject(params)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.