android - 如何在 Android 中使用 json 格式的 RPC 从服务器(PHP)获取数据

标签 android json rpc

我是 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/

相关文章:

android - 在 Android 上使用自己的推特身份验证

php - PHP 中表(JSON 数据)中列的总和

javascript - 如何从 json 文件加载多个标记?

java - "wrong"使用 java Apache XML RPC 3.1.3 服务器响应的数组类型

c# - WMI RPC 服务器不可用。 (来自 HRESULT : 0x800706BA) 的异常

android - 一段时间后 Firebase onDataChange 未调用打开应用程序

android - 当 EditText 获得焦点时显示键盘,失去焦点时隐藏键盘

asp.net - 如何从 ASP.NET Web 服务实现自定义 JSON 序列化?

c - 如何通过 RPC 发送数组?

java - Google Map v2 缩放以适合所有标记和多段线