两天后我就遇到了这个问题,但我找不到解决这个问题的方法......
我使用以下 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