我是 Android 新手。我正在尝试制作一个 RPC
以从 PHP
获取数据并以 JSON
格式获取。一切顺利,但我没有收到任何数据作为回应。下面是我的安卓代码
安卓代码
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);
}
}
?>
如果我在这里做了什么,请告诉我
最佳答案
我认为你误用了实体元素。检查我的代码中有什么:
StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK ){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); response.getEntity().consumeContent(); out.close(); return out.toString(); }
关于android - 如何在 Android 中使用 json 格式的 RPC 从服务器(PHP)获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6410336/