php 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!
这里是:更新:更新:
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
# 1 楼答案
找到答案!只需将字符串添加到writebytes
out.writeBytes("data="+jsonParam.toString());
谢谢@NeriaNachum&@杰森