2016-04-27 67 views
0

我新和任何機構可以編寫一個簡單的代碼,用於發送具有可變的「數據」下面的網絡服務器鏈接JSON請求,然後取JSON響應,例還給出,我正在與該信息Android:如何發送帶有變量的json請求並使用POST方法從webserver讀取json響應?

給出

http://teespire.com/ptracking/post/index.php?tag=GetDeviceInfo

JSON請求(發表在主體具有可變的 「數據」)

{ 「做」: 「1」 }

JSON響應

{ 「狀態」:200, 「STATUS_MESSAGE」: 「成功」, 「數據」:{ 「做」: 「1」, 「顯示名」: 「馬利克卡姆蘭」, 「dtoken」 :「sdfhskdflkasjdfklajslkdfj」, 「dtype」:「ios」, 「dateadded」:「2015-04-25 15:47:34 -0700」, 「country」:「Pakistan」, 「continent」:「Asia 」 「LAT」: 「123.46477」, 「LNG」: 「123.46477」 }}

+2

[讓我爲你谷歌](https://developer.android.com/training/volley/index.html) – n00dl3

+1

你應該檢查這個網站http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ 它已經詳細解釋了該做什麼以及如何做一步明智。 – am110787

回答

0

從非ui線程調用以下方法並傳遞url和body內容。在doInBackground方法中使用AsyncTask並調用以下方法。

class BackgroundTask extends AsyncTask<Void,Void,String>{ 

     @Override 
     protected String doInBackground(Void... voids) { 

      String url = "http://teespire.com/ptracking/post/index.php?tag=GetDeviceInfo"; 
      String body = "{ \"did\": \"1\" }"; 

      return excutePost(url,body); 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      if(result != null){ 
       try { 
        JSONObject jsonObject = new JSONObject(result); 

        int status = jsonObject.getInt("status"); 
        JSONObject dataObject = jsonObject.getJSONObject("data"); 
        String displayName = dataObject.getString("displayname"); 


        //do your stuff here 

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




public String excutePost(String targetURL, String body) 
     { 
     URL url; 
     HttpURLConnection connection = null; 
     try { 
      //Create connection 
      url = new URL(targetURL); 
      connection = (HttpURLConnection)url.openConnection(); 
      connection.setRequestMethod("POST"); 
      connection.setRequestProperty("Content-Type", 
       "application/json"); 

      connection.setUseCaches (false); 
      connection.setDoInput(true); 
      connection.setDoOutput(true); 

      //Send request 
      DataOutputStream wr = new DataOutputStream (
         connection.getOutputStream()); 
      wr.writeBytes (body); 
      wr.flush(); 
      wr.close(); 

      if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){ 
      //Get Response  
       InputStream is = connection.getInputStream(); 
       BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
       String line; 
       StringBuffer response = new StringBuffer(); 
       while((line = rd.readLine()) != null) { 
       response.append(line); 
       response.append('\r'); 
       } 
       rd.close(); 
       return response.toString(); 
      }else{ 
       InputStream is = connection.getErrorStream(); 
       BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
       String line; 
       StringBuffer response = new StringBuffer(); 
       while((line = rd.readLine()) != null) { 
       response.append(line); 
       response.append('\r'); 
       } 
       rd.close(); 
      } 

     } catch (Exception e) { 

      e.printStackTrace(); 
      return null; 

     } finally { 

      if(connection != null) { 
      connection.disconnect(); 
      } 
     } 
     } 

我認爲這對你有幫助。

+0

爵士它給文件中未發現異常,輸入流, – ASIM

+0

我檢查通過郵遞員,它返回{ 「狀態」:402, 「STATUS_MESSAGE」:「參數丟失」, 「數據」:空 } – USKMobility

+0

HTTP :?//teespire.com/ptracking/post/index.php標籤= GetTrackerInfo { 「TID」: 「1」 } 它給這個環節上同樣的錯誤也將web服務器上對此作出迴應, 而這個網絡服務器在其他設備上運行良好 – ASIM

相關問題