2016-02-27 113 views
1

我試圖發送數據到服務器並讀取服務器消息作爲響應。後端在PHP中,工作正常。但是,無論何時我試圖從android發送數據,$_GET$_POST$_RESPONSE陣列都將保持爲空。無法發送GET/POST請求到服務器

的doInBackground是:

@Override 
     protected Void doInBackground(Void... params) { 
      Log.d(TAG,"Inside do in background"); 

      try { 

       Log.d(TAG,"Here is new url."); 
       URL url = new URL("http://192.168.1.33/post/home.php"); 

       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 


       urlConnection.setRequestMethod("GET"); 
       urlConnection.setDoInput(true); 
       urlConnection.setDoOutput(true); 
       urlConnection.setRequestProperty("Content-Type", "application/json"); 


//sending data in json format 

       JSONObject jsonObject = new JSONObject(); 

       jsonObject.put("name","salman Khan"); 

       jsonObject.put("password","khankhan"); 

       String sendingString ; 

       sendingString = jsonObject.toString(); 

       byte[] outputBytes = sendingString.getBytes("UTF-8"); 

       Log.d(TAG, "Output bytes are " + sendingString); 

       OutputStream outputStream = urlConnection.getOutputStream(); 

       Log.d(TAG," got output stream."); 
       outputStream.write(outputBytes); 

       Log.d(TAG,"I am waiting for response code"); 
       int responseCode = urlConnection.getResponseCode(); 

       Log.d(TAG,"response code: " + responseCode); 

       if (responseCode == HttpURLConnection.HTTP_OK) 
       { 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 

        String line; 

        Log.d(TAG,"Before reading the line."); 

        while ((line = bufferedReader.readLine()) != null) 
        { 
         Log.d(TAG, " The line read is: " + line); 
        } 
       } 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

PHP代碼:

<?php 

     $response = array(); 
     // print_r($_GET); 
     print_r($_REQUEST); 
     if (!empty($_GET['name']) || !empty($_GET['password'])) 
     { 
       // echo "I am in true."; 
       $response['name'] = $_GET['name']; 
       $response['password'] = $_GET['password']; 
       $response['status'] = true; 
     } 
     else 
     { 
       $response['status'] = false; 
     } 
     echo json_encode($response); 
?> 

我得到以下日誌:

964 21872-21895/com.example.khan.post_interaction D/shahjahan: got output stream. 
02-27 12:09:51.964 21872-21895/com.example.khan.post_interaction D/shahjahan: I am waiting for response code 
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: response code: 200 
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: Before reading the line. 
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: The line read is: Array 
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: The line read is: (
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: The line read is:) 
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: The line read is: {"status":false} 
+0

如果您通過JSON發送數據而不是後置字段,那麼它的數據將不在$ _POST中。你必須從stdin中讀取它。 – Devon

+0

基本上我想發送$ _POST數組。請引導我一樣。 – learner

+2

不知道Android,所以我不能指導你。但我可以告訴你,它看起來像你發送數據作爲application/json。如果是這樣的話,那麼你需要在PHP中正確讀入數據或以不同的方式發送數據。 – Devon

回答

1

入住manifes添加下列權限文件

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
+0

我已經添加了它。應用程序正與服務器通信。 – learner

+0

真棒學習者 –

0

使用抽射,用於傳輸數據

http://developer.android.com/intl/es/training/volley/index.html

在您的請求類(即擴展請求),覆蓋getParams的最佳方式()方法。你可以做同樣的標題,只需重寫getHeaders()。

相關問題