Я использую этот код для отправки данных с объектом JSON, но мой код говорит, что я отправил ноль 'jsonResponse is always null'
. Я не вижу, где моя проблема, я не уверен с этой строкой кода
String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());
Класс EntityUtils устарел в Android.
public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> {
String url = "http://192.168.52.170/socketIO/examples/chat/public/";
@Override
protected JSONObject doInBackground(JSONObject... data) {
JSONObject json = data[0];
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);
JSONObject jsonResponse = null;
HttpPost post = new HttpPost(url);
try {
StringEntity se = new StringEntity("json="+json.toString());
post.addHeader("content-type", "application/json");
post.setEntity(se);
HttpResponse response;
response = client.execute(post);
String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());
jsonResponse = new JSONObject(resFromServer);
Log.i("Response from server", jsonResponse.getString("msg"));
} catch (Exception e) { e.printStackTrace();}
return jsonResponse;
}
}