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

时间:2022-10-22 22:30:00

I am new in Android. I am trying to make an RPC to fetch data from PHP and get in JSON format. Everything goes fine but I am not getting any data as response. Bellow is my android code

我是Android新手。我正在尝试使用RPC从PHP获取数据并以JSON格式获取数据。一切都很好,但我没有得到任何作为回应的数据。下面是我的android代码

Android Code

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 Code

<?php
class TestJava
{
public function getData()  
{  
   $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
   echo json_encode($arr);
} 
}
?>

Please let me know if anything I did here

如果我在这里做了什么,请告诉我。

2 个解决方案

#1


1  

I think you misused entity element. Check what I have in my code:

我认为你误用了实体元素。检查我的代码:

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();

}

#2


0  

I am a little surprised how you could retrieve any information from this php script. This script doesnt output anything. Try it like this:

我对如何从这个php脚本中检索任何信息感到有点惊讶。这个脚本没有输出任何内容。试试这样:

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>

The second "strange" thing is your way of receiving data in your app. You should read a input stream instead of writing into a output stream... Or did I completely miss something?!

第二个“奇怪”的事情是你在应用程序中接收数据的方式。还是我完全错过了什么?

#1


1  

I think you misused entity element. Check what I have in my code:

我认为你误用了实体元素。检查我的代码:

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();

}

#2


0  

I am a little surprised how you could retrieve any information from this php script. This script doesnt output anything. Try it like this:

我对如何从这个php脚本中检索任何信息感到有点惊讶。这个脚本没有输出任何内容。试试这样:

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>

The second "strange" thing is your way of receiving data in your app. You should read a input stream instead of writing into a output stream... Or did I completely miss something?!

第二个“奇怪”的事情是你在应用程序中接收数据的方式。还是我完全错过了什么?