2016-08-10 44 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

相关问题