Android Web服务访问问题

时间:2012-08-20 08:19:17

标签: 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 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));
 ?>

当我运行此代码时,它会出错!  我的代码中的错误是什么?我已经尝试了很多次但是找不到任何错误。请帮忙!如何解决这个问题

谢谢!

错误堆栈跟踪

08-11 21:52:11.120: W/KeyCharacterMap(3293): No keyboard for id 0
08-11 21:52:11.120: W/KeyCharacterMap(3293): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-11 21:58:51.352: I/global(3357): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
08-11 21:58:51.362: D/AndroidRuntime(3357): Shutting down VM
08-11 21:58:51.371: W/dalvikvm(3357): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-11 21:58:51.381: E/AndroidRuntime(3357): FATAL EXCEPTION: main
08-11 21:58:51.381: E/AndroidRuntime(3357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.json.ws/com.json.ws.JsonV01Activity}: java.lang.ClassCastException: java.lang.String
08-11 21:58:51.381: E/AndroidRuntime(3357):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at android.os.Looper.loop(Looper.java:123)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at java.lang.reflect.Method.invokeNative(Native Method)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at java.lang.reflect.Method.invoke(Method.java:521)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at dalvik.system.NativeStart.main(Native Method)
08-11 21:58:51.381: E/AndroidRuntime(3357): Caused by: java.lang.ClassCastException: java.lang.String
08-11 21:58:51.381: E/AndroidRuntime(3357):     at com.json.ws.JsonV01Activity.onCreate(JsonV01Activity.java:45)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-11 21:58:51.381: E/AndroidRuntime(3357):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-11 21:58:51.381: E/AndroidRuntime(3357):     ... 11 more

3 个答案:

答案 0 :(得分:1)

试试这个。

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"));
 ?>

答案 1 :(得分:1)

你在已经json_encoded的字符串上调用json_encode。

将其更改为此或类似:

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

答案 2 :(得分:0)

确定。然后检查json是否有问题。

在php文件中打印只使用echo的名称。

并尝试将该文本添加到textview