JSON异常:String无法在android中转换为JSONObject

时间:2014-02-27 07:28:12

标签: android json listview android-listview

我使用JSON从mysql数据库中获取数据并在LIstview Item中设置数据。我的Java代码工作正常。但我无法在listview中获得价值。如何转换json值。

我怎么能这样做。

      02-27 12:47:17.548: W/System.err(637): org.json.JSONException: Value <!DOCTYPEHTMLPUBLIC"- of type java.lang.String cannot be converted to JSONObject
      02-27 12:47:17.557: W/System.err(637):    at  org.json.JSON.typeMismatch(JSON.java:107)
      02-27 12:47:17.557: W/System.err(637):    at org.json.JSONObject.<init>(JSONObject.java:158)
      02-27 12:47:17.557: W/System.err(637):    at org.json.JSONObject.<init>(JSONObject.java:171)
      02-27 12:47:17.557: W/System.err(637):    at com.amplio.upshot.AddToOutlet.onCreate(AddToOutlet.java:42)
      02-27 12:47:17.557: W/System.err(637):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
      02-27 12:47:17.568: W/System.err(637):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
      02-27 12:47:17.568: W/System.err(637):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
      02-27 12:47:17.568: W/System.err(637):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
      02-27 12:47:17.568: W/System.err(637):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
      02-27 12:47:17.568: W/System.err(637):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
      02-27 12:47:17.568: W/System.err(637):    at android.widget.TabHost.setCurrentTab(TabHost.java:323)
      02-27 12:47:17.568: W/System.err(637):    at com.amplio.upshot.MainActivity.onCreate(MainActivity.java:112)
      02-27 12:47:17.568: W/System.err(637):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
      02-27 12:47:17.578: W/System.err(637):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
      02-27 12:47:17.578: W/System.err(637):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
      02-27 12:47:17.578: W/System.err(637):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
      02-27 12:47:17.578: W/System.err(637):    at           android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
      02-27 12:47:17.578: W/System.err(637):    at android.os.Handler.dispatchMessage(Handler.java:99)
      02-27 12:47:17.578: W/System.err(637):    at android.os.Looper.loop(Looper.java:123)
      02-27 12:47:17.578: W/System.err(637):    at android.app.ActivityThread.main(ActivityThread.java:4627)
      02-27 12:47:17.578: W/System.err(637):    at java.lang.reflect.Method.invokeNative(Native Method)
      02-27 12:47:17.578: W/System.err(637):    at java.lang.reflect.Method.invoke(Method.java:521)
      02-27 12:47:17.578: W/System.err(637):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
      02-27 12:47:17.578: W/System.err(637):    at         com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
      02-27 12:47:17.588: W/System.err(637):    at dalvik.system.NativeStart.main(Native Method)

我的JSON结果

{
"customer": [
    {
        "success": "1",
        "bus_name": "radeonpixels",
        "bus_id": "B108"
    },
    {
        "success": "1",
        "bus_name": "Om Sweets",
        "bus_id": "B109"
    }
]
}

AddToOutlet.java

 public class AddToOutlet extends Activity {
SessionManager session;
String success, cus_id, bus_id;
ArrayList<Item> item;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addtooutlet);
    session = new SessionManager(getApplicationContext());
    session.checkLoginback();

    item = new ArrayList<Item>();
    // get user data from session
    HashMap<String, String> user = session.getUserDetails();
    // ID
    final String cus_id = user.get(SessionManager.KEY_ID);
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("cus_id", cus_id));
    String response = null;
    try {
        response = LoginHttpClient.executeHttpPost(
                "http://10.0.2.2/android_api/add_to_outlet.php",
                postParameters);
        response = response.toString();
        response = response.replaceAll("\\s+", "");
        JSONObject json = new JSONObject(response);
        JSONArray jArray = json.getJSONArray("customer");
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            Item it = new Item();
            it.setBusname(json_data.getString("bus_name"));
            it.setBusid(json_data.getString("bus_id"));

            item.add(it);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    ListView lv = (ListView) findViewById(R.id.list);
    MyAdapter adapter = new MyAdapter(AddToOutlet.this,
            R.layout.addtooutlet_list_item, item);
    lv.setAdapter(adapter);
}
}

1 个答案:

答案 0 :(得分:1)

首先使用适当的内容和标准代码。

编写基本网络发布操作,如:

HttpClient client = new DefaultHttpClient();
HttpPost postStuff = new HttpPost("http://10.0.2.2/android_api/add_to_outlet.php");

然后使用post执行客户端并将其保存在 HttpResponse 变量中,而不是在简单的字符串中。

你做了什么.. String response = null;

但你必须这样做:

 HttpResponse response = client.execute(postStuff); 

现在您可以让服务器通过response.getEntity().getContent()返回,或者您也可以检查其状态,如response.getStatusLine.getStatusCode()

问题:

    response = response.toString();
    response = response.replaceAll("\\s+", "");

这里删除字符串中的所有空格,因此很难识别JsonObject。而且我认为这会解决问题。

如果返回类型完美,那么您的代码就像:

HttpClient client = new DefaultHttpClient();
HttpPost postStuff = new HttpPost("http://10.0.2.2/android_api/add_to_outlet.php");
HttpResponse response = null;
try{
response = client.execute(postStuff);
Bufferedreader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent());

String mReturnDta = "";
while((data = reader.readLine()) != null){
JsonObject json = new JsonObject(mReturnDta);
//Now fetch the jsonarray and do whatever you want...
}    
}catch(...)