Java 如何从Android读取echo语句
目前我只能实际读取包含json格式的php文件 我要做的是读取echo语句(json格式),而不是读取实际文件 这是我到目前为止的android代码,用于请求包含json格式的php文件供我阅读:Java 如何从Android读取echo语句,java,php,android,json,Java,Php,Android,Json,目前我只能实际读取包含json格式的php文件 我要做的是读取echo语句(json格式),而不是读取实际文件 这是我到目前为止的android代码,用于请求包含json格式的php文件供我阅读: // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpCli
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent(); // This is storing the contents from the php file
提前谢谢
这是php文件:
{
"user": [
{
"id": "001",
"name": "Raj Amal",
"email": "raj.amalw@gmail.com"
}
]
}
I want to do this:
<?php
echo '{
"user": [
{
"id": "001",
"name": "Raj Amal",
"email": "raj.amalw@gmail.com"
}
]
}';
?>
{
“用户”:[
{
“id”:“001”,
“姓名”:“Raj Amal”,
“电子邮件”:“raj。amalw@gmail.com"
}
]
}
我想这样做:
尝试如下操作
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
现在可以在httpResponse
中找到服务器的响应。您可以将其转换为如下所示的字符串
String myResponseString = EntityUtils.toString(httpResponse.getEntity());
记录字符串
myResponseString
以查看服务器的响应我认为您的问题在于服务器配置。意思-服务器将普通php(json)识别为json并返回相应的头。
而您的echo以纯文本的形式返回,不包含由HttpEntity指定的标题(已弃用!)
在客户端,上面关于HttpClient的答案是可行的
在服务器端:
<?PHP
$data = /** put your content here! **/;
header('Content-Type: application/json');
echo json_encode($data);
我不太清楚你在问什么-你得到了什么样的回应,为什么你不能阅读?你需要解析JSON还是什么?@Web.11,更新了我的答案<代码>响应
应为httpResponse
。