Java HttpUrlConnection 未发布数据
Java HttpUrlConnection not posting data
世界各地的朋友,
也许我问的都是同一个问题,但我已经搜索了几个小时但仍未找到答案,
我正在尝试post一个对象到服务器,它居然能联系到服务器,但是没有数据发送到服务器,请赐教,
这是我的 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
已找到答案!只需将字符串添加到 writebytes out.writeBytes("data="+jsonParam.toString());
感谢@NeriaNachum 和@Jason
世界各地的朋友,
也许我问的都是同一个问题,但我已经搜索了几个小时但仍未找到答案,
我正在尝试post一个对象到服务器,它居然能联系到服务器,但是没有数据发送到服务器,请赐教,
这是我的 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
已找到答案!只需将字符串添加到 writebytes out.writeBytes("data="+jsonParam.toString());
感谢@NeriaNachum 和@Jason