douci2516 2012-08-20 08:19
浏览 48
已采纳

Android Web服务访问问题

I'm having this problem since two days but I can't find a way to correct this....

I have used following Android http client to access a php web service which has json encoding hosted in localhost.

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;
   }
}

The php code

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

When I'm running this code it gives an error! What is the error in my code ?? I have tried many times but can't find any error. Please help! how to solve this problem

Thanks!

The error stack trace

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条回答 默认 最新

  • douyue8364 2012-08-20 08:42
    关注

    You're calling json_encode on a string that's already json_encoded.

    Change it to this or similar :

    <?php
    $data = array('name' => 'John Adams', 'Age' => '24'); 
    print (json_encode($data)); 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助