2016-08-10 70 views
0

世界各地的朋友,Java的HttpURLConnection類不發佈數據

也許我問同一個問題都結束了,但我已經尋找了幾個小時,但還沒有找到答案,

我想發佈對象到服務器,它實際上能夠聯繫服務器,但沒有向服務器發送數據,請賜教,

這裏是我的Java代碼

protected String doInBackground(String... params) { 
     URL url = null; 
     try { 
      String location = URLEncoder.encode(params[0],"UTF-8"); 
      url = new URL("SERVER ADDRESS"); 
      con = (HttpsURLConnection) url.openConnection(); 
      con.setRequestMethod("POST"); 
      con.setRequestProperty("Content-Type","application/json"); 
      con.setRequestProperty("Accept", "*/*"); 
      con.setDoOutput(true); 
      con.setDoInput(true); 
      OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream()); 
      JSONObject jsonParam = new JSONObject(); 
      jsonParam.put("ID", "25"); 
      jsonParam.put("description", "Real"); 
      jsonParam.put("enable", "true"); 
      out.write(jsonParam.toString()); 
      out.flush(); 
      Log.i("aware", jsonParam.toString()); 
      InputStream stream = con.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 

      StringBuffer buffer = new StringBuffer(); 

      String line = ""; 

      while((line=reader.readLine()) != null) { 
       buffer.append(line); 
      } 

      String json = buffer.toString(); 
      Log.i("json", json); 
      // handle issues 
      int statusCode = con.getResponseCode(); 
      Log.e("Response", "The response is: " + statusCode); 
      out.close(); 
      reader.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

這是PHP代碼

header('Content-Type: application/json'); 
$response = array(); 

if(!empty($_REQUEST)) { 
    $myPost = json_encode($_REQUEST); 
    echo $myPost; 
    $myfile = fopen("request.txt", "w") or die("Unable to open file!"); 
    fwrite($myfile, $myPost); 
    fclose($myfile); 
    $response['statusget'] = "good"; 
} 

if(!empty($_GET)) { 
    $myPost = json_encode($_GET); 
    echo $myPost; 
    $myfile = fopen("get.txt", "w") or die("Unable to open file!"); 
    fwrite($myfile, $myPost); 
    fclose($myfile); 
    $response['statusget'] = "good"; 
} 
if(!empty($_POST)) { 
    $myPost = json_encode($_POST); 
    echo $myPost; 
    $myfile = fopen("post.txt", "w") or die("Unable to open file!"); 
    fwrite($myfile, $myPost); 
    fclose($myfile); 
    $response['statuspost'] = "good"; 
} 
if(empty($jsonString) && empty($_GET) && empty($_POST)) { 
    $response['status'] = "nothing"; 
} 

echo json_encode($response); 

日誌是這樣

08-10 23:44:41.954 29887-30497/com.mulai.smartsquare I/aware: {"ID":"25","description":"Real","enable":"true"} 
08-10 23:44:41.994 29887-30497/com.mulai.smartsquare I/System.out: (HTTPLog)-Static: isSBSettingEnabled false 
08-10 23:44:42.119 29887-30497/com.mulai.smartsquare I/json: {"status":"nothing"} 
08-10 23:44:42.119 29887-30497/com.mulai.smartsquare E/Response: The response is: 200 
08-10 23:44:42.119 29887-29887/com.mulai.smartsquare I/Process: Done! 

這裏是:::::::::::: UPDATE :::::::::: :::

HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
      con.setRequestMethod("POST"); 
      con.setRequestProperty("Content-Type","application/json"); 
      con.setRequestProperty("Accept", "application/json"); 
      con.setDoOutput(true); 
      con.connect(); 
      DataOutputStream out = new DataOutputStream(con.getOutputStream()); 
      JSONObject jsonParam = new JSONObject(); 
      jsonParam.put("ID", "25"); 
      jsonParam.put("description", "Real"); 
      jsonParam.put("enable", "true"); 
      out.writeBytes(jsonParam.toString()); 
      Log.i("Posting...", jsonParam.toString()); 
      out.flush(); 
      out.close(); 
      Log.i("HTTP URL Connection", "POSTING"); 
      InputStream stream = con.getInputStream(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); 

      StringBuffer buffer = new StringBuffer(); 

      String line = ""; 

      while((line=reader.readLine()) != null) { 
       buffer.append(line); 
      } 

      String json = buffer.toString(); 
      Log.i("json", json); 
      int statusCode = con.getResponseCode(); 
      Log.e("Response", "The response is: " + statusCode); 

響應是發現

08-15 16:47:11.188 20709-21742/com.mulai.smartsquare I/System.out: (HTTPLog)-Static: isSBSettingEnabled false 
    08-15 16:47:11.188 20709-21742/com.mulai.smartsquare I/System.out: (HTTPLog)-Static: isShipBuild true 
    08-15 16:47:11.188 20709-21742/com.mulai.smartsquare I/System.out: (HTTPLog)-Thread-17850-125518020: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false 
    08-15 16:47:11.188 20709-21742/com.mulai.smartsquare I/System.out: (HTTPLog)-Static: isSBSettingEnabled false 
    08-15 16:47:11.488 20709-21742/com.mulai.smartsquare I/Posting...: {"ID":"25","description":"Real","enable":"true"} 
    08-15 16:47:11.488 20709-21742/com.mulai.smartsquare I/HTTP URL Connection: POSTING 
    08-15 16:47:11.568 20709-21742/com.mulai.smartsquare I/System.out: (HTTPLog)-Static: isSBSettingEnabled false 
    08-15 16:47:11.648 20709-21742/com.mulai.smartsquare I/json: {"status":"nothing"} 
    08-15 16:47:11.648 20709-21742/com.mulai.smartsquare E/Response: The response is: 200 
+0

兩件事情:1,嘗試使請求之前關閉的作家。 2.嘗試使用二進制書寫而不是文字書寫('DataOutputStream'而不是'OutputStreamWriter')。 –

+0

好的,謝謝@NeriaNachum,我會試試看,並讓你知道... – Kerosky

+0

試過了,但它顯示'無法解決方法寫()'當我改變'OutputStreamWriter'爲'DataOutputStream' – Kerosky

回答

0

回答!只需添加字符串writebytes out.writeBytes("data="+jsonParam.toString());

感謝@NeriaNachum & @Jason

相關問題