androidandroid-networkingandroidhttpclient

How to HTTP POST in Android Studio


I'm trying to do a HTTP POST in Android studio using with to retrieve values. I have already tested the thing in POSTMAN but I'm unsure of how to type it in Android studio. Please assist me in creating the HTTP POST Code for this.

i'm doing a POST to

ml2.internalpositioning.com/track

with this body

{"username":"fyp","location":"location","group":"cowardlycrab","time":1501640084739,"wifi-fingerprint":[{"mac":"04:c5:a4:66:43:7k","rssi":-29}]}

Solution

  •  //call asynctask  like below : 
    
    JSONObject post_dict = new JSONObject();
                        try {
                            post_dict.put("username", "your_username_data");
     post_dict.put("location", "your_location_data");
     post_dict.put("group", "your_group_data");
     post_dict.put("time", "your_time_data");
    
    JSONArray jarr = new JSONArray();
    JSONObject jonj = new JSONObject();
    jonj.put("mac","your_mac_data");
    jonj.put("rssi","your_rssi_data");
    jarr.put(jonj);
    post_dict.put("wifi-fingerprint", jarr);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        new YourAsyncTask().execute(String.valueOf(post_dict));
    
    
    
    //Actual Async Task Class 
    
          public class YourAsyncTask extends AsyncTask<String, String, String> {
                ProgressDialog progressDialog;
    
                protected void onPreExecute() {
                    progressDialog = ProgressDialog.show(MainActivity.this,
                            "Please Wait...",
                            "Registering Device");
                    super.onPreExecute();
                }
    
                protected String doInBackground(String... params) {
                    String JsonResponse = null;
                    String JsonDATA = params[0];
                    HttpURLConnection urlConnection = null;
                    BufferedReader reader = null;
                    try {
                        URL url = new URL("ml2.internalpositioning.com/track");
                        urlConnection = (HttpURLConnection) url.openConnection();
                        urlConnection.setDoOutput(true);
                        // is output buffer writter
                        urlConnection.setRequestMethod("POST");
                        urlConnection.setRequestProperty("Content-Type", "application/json");
                        urlConnection.setRequestProperty("Accept", "application/json");
                        //set headers and method
                        Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
                        writer.write(JsonDATA);
                        // json data
                        writer.close();
                        InputStream inputStream = urlConnection.getInputStream();
                        //input stream
                        StringBuffer buffer = new StringBuffer();
                        if (inputStream == null) {
                            // Nothing to do.
                            return null;
                        }
                        reader = new BufferedReader(new InputStreamReader(inputStream));
    
                        String inputLine;
                        while ((inputLine = reader.readLine()) != null)
                            buffer.append(inputLine + "\n");
                        if (buffer.length() == 0) {
                            // Stream was empty. No point in parsing.
                            return null;
                        }
                        JsonResponse = buffer.toString();
                        //response data
                        try {
                        //send to post execute
                            return JsonResponse;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
    
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (urlConnection != null) {
                            urlConnection.disconnect();
                        }
                        if (reader != null) {
                            try {
                                reader.close();
                            } catch (final IOException e) {
                            }
                        }
                    }
                    return null;
                }
    
                @Override
                protected void onPostExecute(String result) {
                    if (progressDialog != null)
                        progressDialog.dismiss();
                    super.onPostExecute(result);
                   //Do something with result
                }
            }