Kerosky Kerosky - 3 months ago 36
Java Question

Java HttpUrlConnection not posting data

friends around the world,

Maybe I am asking the same question all over, but I have searched for a few hours but yet to find the answer,

I am trying to post an object to the server, it actually able to contact the server, but there is no data sent to the server, please enlighten me,

Here is my Java code

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;
}


This is the PHP Code

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);


The log is like this

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!


HERE is ::::::::::::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);


Response is

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

Answer

Answer found! just add string to writebytes out.writeBytes("data="+jsonParam.toString());

Thanks @NeriaNachum & @Jason

Comments