Reputation: 2535
I am trying to send data to php side from my Android client below is the code:
(I have constructed this code from various sources on the internet so I am not sure if all is well here)
private void postJSON(String myurl) throws IOException {
java.util.Date date= new java.util.Date();
Timestamp timestamp = (new Timestamp(date.getTime()));
try {
JSONObject parameters = new JSONObject();
parameters.put("timestamp",timestamp);
parameters.put("jsonArray", new JSONArray(Arrays.asList(makeJSON())));
parameters.put("type", "Android");
parameters.put("mail", "[email protected]");
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setReadTimeout(10000 /* milliseconds */);
// conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestProperty( "Content-Type", "application/json" );
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(parameters.toString());
writer.close();
out.close();
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}catch (Exception exception) {
System.out.println("Exception: "+exception);
}
}
I am confused at this line:
writer.write(parameters.toString());
essentially I am only sending one string to the php side. How do I receive it there? What would be the POST variable name?
Upvotes: 0
Views: 598
Reputation: 55
like CarlosAllende i have no experience in Android. But hope this might help you.
this is what you get in your '$_POST' ?
Response Code : 200 01-21 10:46:44.969: I/System.out(21561): <br />
<b>Catchable fatal error</b>: Object of class stdClass could not be converted to string in
<b>/opt/lampp/htdocs/Pdrive/digital_anniversaries/wordpress-3.9.1/wordpress/authenticate_user.php</b> on line <b>15</b><br />
if so it is a Catchable fatal error telling that 'Object of class stdClass could not be converted to string'. so if i am correct it's trying to take object into your $_POST array and it's is unable to convert it to string. and instead your getting the error in your $_POST array.
hope this helps
Upvotes: 1
Reputation: 861
It looks like you are sending binary data instead of name-value
post parameters.
Try this in you php code : echo file_get_contents("php://input");
and see what it prints.
additionally, You can either use HTTPClient
or RestHTTPClient from loopj
as you android client.
Upvotes: 1
Reputation: 292
I have absolutely no experience with android, however I am guessing either an array of some kind or as json, You can see what it sends by making a page with <?php echo var_dump($_POST); ?>
on it and sending the POST request to the page.
Upvotes: 1