JSONArray无法转换为JSONObject?

时间:2014-02-24 14:14:26

标签: android json

我每次尝试连接到我的sql数据库时都会收到此错误消息:

JSONArray cannot be converted to JSONObject

从数据库获取数据工作正常,但是当我尝试获取登录用户的数据时,我得到了上述错误。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
    listView  = (ListView)rootView.findViewById(R.id.listView);

    WebView ourBrow = (WebView)rootView.findViewById(R.id.wvBrowser);

    accessWebService();

    return rootView;
}


// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0]);
        try {
            HttpResponse response = httpclient.execute(httppost);
            jsonResult = inputStreamToString(
                    response.getEntity().getContent()).toString();
        }

        catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    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();
            Toast.makeText(getActivity(),
                    "Error..." + e.toString(), Toast.LENGTH_LONG).show();
        }
        return answer;
    }

    @Override
    protected void onPostExecute(String result) {
        ListDrwaer();
    }
}// end async task

public void accessWebService() {
    JsonReadTask task = new JsonReadTask();
    // passes values for the urls string array
    task.execute(new String[] { url });
}

// build hash set for list view
public void ListDrwaer() {
    List<Map<String, String>> userList = new ArrayList<Map<String, String>>();

    try {
        JSONObject json = new JSONObject(jsonResult);
        JSONArray jsonMainNode = json.getJSONArray("users");

        for (int i = 0; i < jsonMainNode.length(); i++) {
            JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
            String id = jsonChildNode.optString("id");
            String name = jsonChildNode.optString("username");
           String adr = jsonChildNode.optString("adres");
           // String number = jsonChildNode.optString("password");
            String outPut = name + "-" + id + "-" + adr;
            userList.add(create_user("id","username", outPut));
        }
    } catch (JSONException e) {
        Toast.makeText(getActivity(), "Error" + e.toString(),
                Toast.LENGTH_SHORT).show();
    }

    SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), userList,
            android.R.layout.simple_list_item_1,
            new String[] { "id"}, new int[] { android.R.id.text1 });

    listView.setAdapter(simpleAdapter);
}

private HashMap<String, String> create_user(String name,String id, String adr) {
    HashMap<String, String> userNo = new HashMap<String, String>();
    userNo.put(name, adr);
    return userNo;
}

}

这是logcat:

02-25 10:26:42.690    5233-5233/com.example.app W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
02-25 10:26:42.690    5233-5233/com.example.app W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:158)
02-25 10:26:42.690    5233-5233/com.example.app W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:171)
02-25 10:26:42.690    5233-5233/com.example.app W/System.err﹕ at com.example.app.MoviesFragment.ListDrwaer(MoviesFragment.java:110)
02-25 10:26:42.690    5233-5233/com.example.app W/System.err﹕ at com.example.app.MoviesFragment$JsonReadTask.onPostExecute(MoviesFragment.java:95)
02-25 10:26:42.690    5233-5233/com.example.app W/System.err﹕ at com.example.app.MoviesFragment$JsonReadTask.onPostExecute(MoviesFragment.java:55)
02-25 10:26:42.690    5233-5233/com.example.app W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:631)
02-25 10:26:42.690    5233-5233/com.example.app W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-25 10:26:42.700    5233-5233/com.example.app W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-25 10:26:42.700    5233-5233/com.example.app W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 10:26:42.700    5233-5233/com.example.app W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
02-25 10:26:42.700    5233-5233/com.example.app W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4931)
02-25 10:26:42.700    5233-5233/com.example.app W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-25 10:26:42.700    5233-5233/com.example.app W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
02-25 10:26:42.700    5233-5233/com.example.app W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-25 10:26:42.700    5233-5233/com.example.app W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-25 10:26:42.700    5233-5233/com.example.app W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

String id = jsonChildNode.optString("id");

我认为此代码会抛出异常typeMismatch

试试这个:

int id = jsonChildNode.getInt("id");
String usrNm = jsonChildNode.getString("username");
String adres = jsonChildNode.getString("adres");