我是Android新手。我正在尝试创建一个RPC来从PHP获取数据,并将其转换为JSON格式。一切都很顺利,但我没有得到任何数据作为响应。Bellow是我的android代码
Android代码
try{
HttpClient httpClient = new DefaultHttpClient();
JSONObject jsonRequest = new JSONObject();
jsonRequest.put("id", 0);
jsonRequest.put("method", "getData");
HttpEntity entity = new StringEntity(jsonRequest.toString());
HttpPost request = new HttpPost("http://121.247.130.30:8080/test/TestJava.php");
request.setEntity(entity);
HttpResponse response = httpClient.execute(request);
String temp = EntityUtils.toString(response.getEntity());
Log.e("Class====","Error:"+temp);
}
catch(Exception e){
Log.e("Class====","Error:"+e.getMessage());
}PHP代码
<?php
class TestJava
{
public function getData()
{
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
}
}
?>如果我在这里做了什么请告诉我
发布于 2011-06-29 23:24:11
我有点惊讶你怎么能从这个php脚本中检索到任何信息。此脚本不会输出任何内容。像这样试一下:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>第二件“奇怪”的事情是你在你的应用中接收数据的方式。您应该读取输入流,而不是写入输出流...还是我完全漏掉了什么?!
https://stackoverflow.com/questions/6410336
复制相似问题