2017-08-08 18 views
0

私は値を取得するためにwithを使用してAndroidスタジオでHTTP POSTを実行しようとしています。私はすでにPOSTMANでそのことをテストしましたが、私はAndroidスタジオに入力する方法が不明です。このためにHTTP POSTコードを作成するのを手伝ってください。Androidスタジオでの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- i-use-the-simple-http-client-in-android –

+0

@Héctorもし私が答えたら、私はPOSTMANで使っているボディをどこに置くのですか? –

+0

2番目の答えを見てください。私たちが何かをリンクさせるときは、全体を読むべきです。 – 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 
      } 
     } 
関連する問題