Issue
I try to read data from json send by HttpURLConnection.
Activity.
Gson gson = new GsonBuilder().create();
String json = gson.toJson(cDati);
if(FunzioniSalvataggio.getInstance(mContext).saveDataToSend(json,"InizioServizio_"))
new FunzioniInvio().execute(json);
AsyncTask.
URL paginaURL = new URL("http:***");
HttpURLConnection client = (HttpURLConnection) paginaURL.openConnection();
client.setChunkedStreamingMode(0);
client.setDoOutput(true);
client.setDoInput(true);
client.setRequestProperty("X-Requested-With", "XMLHttpRequest");
client.setRequestProperty("Content-Type", "application/json");
client.setRequestMethod("POST");
client.connect();
OutputStreamWriter wr = new OutputStreamWriter(client.getOutputStream());
wr.write(json);
wr.flush();
InputStream risposta = new BufferedInputStream(client.getInputStream());
dati = mostroDati(risposta);
PHP.
$postdata = file_get_contents("php://input");
$json = json_decode($postdata, true);
But when PHP page try to read it with "php://input", it not found data. How do it?
Solution
I have not used OutputStreamWriter
but I have had success with the DataOutputStream
.
Try this and encode your json string
DataOutputStream wr = new DataOutputStream(urlConn.getOutputStream());
wr.writeBytes(URLEncoder.encode(json,"UTF-8"));
wr.flush();
wr.close();
I have no problem with this code. But, I have heard some people have problems with the wr.close();
statement.
I would suggest that you URL-encode the data you are sending. This can be important if some unusual characters are included in the data you send.
Answered By - Barns
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.