1

its NOT DUPLICATE.Link that has been provided is an OLD one."http client" has been removed in api23

I want to send json object:

{"emailId":"ashish.bhatt@mobimedia.in","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"}

to url:

http://10digimr.mobimedia.in/api/mobile_retailer/update_profile How do i do it? via post method?

METHOD:

 POST /api/mobile_retailer/update_profile

MANDATORY KEY:

{"emailId","address"}

REQUEST JSON:

{"emailId":"ashish.bhatt@mobimedia.in","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"}

RESPONSE:

{"message":"Mail Send","data":true,"status":200}
Matt
  • 14,007
  • 25
  • 88
  • 136
user6092109
  • 23
  • 1
  • 1
  • 7
  • the api you have mentioned is a GET method. Ask the API developer for documentation about the correct way to access the API. I tried sending a POSt request to it via rest client. It gives an error. – crashOveride Apr 25 '16 at 06:58
  • 1
    Possible duplicate of [How to send a JSON object over Request with Android?](http://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android) – Shree Krishna Apr 25 '16 at 07:00
  • my bad! wrong url was written! – user6092109 Apr 25 '16 at 07:03
  • its NOT DUPLICATE.Link that has been provided is an OLD one."http client" has been removed in api23 – user6092109 Apr 25 '16 at 07:24

2 Answers2

5

Define a class AsyncT and call it in onCreate method using:

AsyncT asyncT = new AsyncT();
asyncT.execute();

Class definition:

class AsyncT extends AsyncTask<Void,Void,Void>{

        @Override
        protected Void doInBackground(Void... params) {

            try {
                URL url = new URL(""); //Enter URL here
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setRequestMethod("POST"); // here you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc.
                httpURLConnection.setRequestProperty("Content-Type", "application/json"); // here you are setting the `Content-Type` for the data you are sending which is `application/json`
                httpURLConnection.connect();

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("para_1", "arg_1");

                DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
                wr.writeBytes(jsonObject.toString());
                wr.flush();
                wr.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }


    }
Michał Perłakowski
  • 80,501
  • 25
  • 149
  • 167
0

@Sandip Subedi This is how you get response from the httpURLConnection

class AsyncT extends AsyncTask<Void,Void,Void>{

    @Override
    protected Void doInBackground(Void... params) {

        try {
            URL url = new URL(""); //Enter URL here
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST"); // here you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc.
            httpURLConnection.setRequestProperty("Content-Type", "application/json"); // here you are setting the `Content-Type` for the data you are sending which is `application/json`
            httpURLConnection.connect();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("para_1", "arg_1");

            DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
            wr.writeBytes(jsonObject.toString());
            wr.flush();
            wr.close();

            InputStream response = httpURLConnection.getInputStream();
            BufferedReader reader = new BufferedReader(newInputStreamReader(response);
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                   is.close();
                } catch (IOException e) {
                   e.printStackTrace();
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }


}
terminator
  • 91
  • 5