Merbin Joe
Merbin Joe

Reputation: 121

How can I pass the post data to my php code using android java?

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"}"

Upvotes: 1

Views: 62

Answers (1)

Stephen
Stephen

Reputation: 1148

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

Upvotes: 2

Related Questions