Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Android web服务访问问题_Php_Android_Json - Fatal编程技术网

Php Android web服务访问问题

Php Android web服务访问问题,php,android,json,Php,Android,Json,两天以来我一直有这个问题,但我找不到纠正这个问题的方法 我使用以下Android http客户端访问了一个php web服务,该服务在localhost中托管了json编码 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import android.app.Activity; import androi

两天以来我一直有这个问题,但我找不到纠正这个问题的方法

我使用以下Android http客户端访问了一个php web服务,该服务在localhost中托管了json编码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class JsonV01Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2/json01/phpjson01.php");
    try {

        HttpResponse response = httpclient.execute(httppost);
         String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
        TextView tv = (TextView)findViewById(R.id.textView1);
        JSONObject object = (JSONObject) new JSONTokener(jsonResult).nextValue();
        String name = object.getString("name");
        String age = object.getString("Age");
        tv.append(name);
        tv.append(age);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


   }
private StringBuilder inputStreamToString(InputStream is) {
    String rLine = "";
    StringBuilder answer = new StringBuilder();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    try {
     while ((rLine = rd.readLine()) != null) {
      answer.append(rLine);
       }
    }

    catch (IOException e) {
        e.printStackTrace();
     }
    return answer;
   }
}
php代码

 <?php
   $string='{"name":"John Adams","Age":"24"}';
    print (json_encode($string));
 ?>
试试这个

JSONObject object = new JSONObject(jsonResult);
String name = object.getString("name");
int age = object.getInt("Age");
tv.setText(name + " "  + age);

<?php
    echo json_encode(array("name"=>"John Adams","Age"=>"24"));
 ?>
JSONObject object=新的JSONObject(jsonResult);
字符串名称=object.getString(“名称”);
int age=object.getInt(“age”);
tv.setText(姓名+“”+年龄);

好的。然后检查json是否有任何错误

在php文件中,仅使用echo打印名称


并尝试将该文本添加到textview中,您正在调用json_encode,而该字符串已经是json_编码的

将其更改为此项或类似项:

<?php
$data = array('name' => 'John Adams', 'Age' => '24'); 
print (json_encode($data)); 
?>


“应用程序意外停止…请重试”我已发布错误堆栈跟踪,也请检查!您正在测试哪个android
OS
?已解决:::我所做的唯一一件事就是根据公认的答案更改php web服务:)谢谢大家@Grant,尝试在logcat中记录名称和年龄,并使用tv.setText(name+“”+age);当我使用tv.setText(jsonResult)时,Android中的php输出为“{”name\”:“John Adams\”,“age\”:“24\”}。当我使用tv.setText时,Android中的setText(jsonResult)会显示此字符串“{”name\”:“John Adams\”,“age\”:“24\”,Android文本视图中的php输出仅与JSON有问题。“{”name\”:“John Adams\”,“age\”:“24\”(jsonResult)它在android文本视图中显示字符串“{“name\”:“John Adams\”,““Age\”:“24\”}”,这是json唯一的问题,为什么要将其作为
答案发布?
?你可以在以前的答案下将其作为注释发布。我正在使用手机,这就是为什么要使用新答案。顺便问一下,你是否尝试过没有json的php?
<?php
$data = array('name' => 'John Adams', 'Age' => '24'); 
print (json_encode($data)); 
?>