android - 将 JSON 数据从 Android 发送到 PHP 脚本
问题描述
我正在使用此代码发送带有 JSON 对象的数据,但我的代码说我发送 null '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;
}
}
解决方案
推荐阅读
- windows-server - 将 Windows Server 2016 机器从 GUI 更改为 Core
- php - 使用 PHP 的动态输入字段的错误消息
- python-3.x - 只为 Keras(TF 后端)脚本分配一个 gpu
- python - ByteArray 不显示十六进制数字 Python
- android - 为什么在 Android Studio 上显示“服务器的证书不受信任”?
- .net - VSTS 上的 Intranet TFS
- c# - Xamarin - 尝试共享时找不到文件
- javascript - 如何从 Angular 6 中的组件中提取文本进行翻译(ngx-translate)
- javascript - 如何将“凭据:包含”添加到 d3.json 方法?D3.js
- cocoapods - 无法在 Xcode10 中运行/编译 AudioKit V4.3