2017-08-08 175 views
0

我正在尝试在Android Studio中执行HTTP POST以检索值。我已经在POSTMAN中测试过这个东西,但我不确定如何在Android studio中输入它。请协助我为此创建HTTP POST代码。如何在Android Studio中进行HTTP POST

我做一个POST到

ml2.internalpositioning.com/track 

这身

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

https://stackoverflow.com/questions/4457492/how-do-我使用这个简单的http客户端在安卓 –

+0

@Héctor如果我回答我在哪里把我在POSTMAN中使用的身体? –

+0

看第二个答案。当我们连接某些东西时,应该阅读整个事情。 – csmckelvey

回答

1
//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 
      } 
     }