尝试获取数据时,JSONArray无法转换为JSONObject错误

我有一个Android应用程序,我想通过它读取json以下的json。

[{"name":"ABCD","country":"INDIA","twitter":"abc"},{"name":"nehasharma","country":"india","twitter":"indore"},{"name":"Arif","country":"India","twitter":"@sdf"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"bawender","country":"18","twitter":null},{"name":"dddd","country":"india","twitter":"sdtt"},{"name":"dddd","country":"india","twitter":"sdtt"},{"name":"dddd","country":"india","twitter":"fghjj"},{"name":"je","country":"xe","twitter":"@rtttt.com\n"},{"name":"ajh","country":"eyohio","twitter":"mp"},{"name":"hasan","country":"jsjs","twitter":"snsns"},{"name":"13.3738383383","country":"38.3838383829","twitter":"location"},{"name":"Latitude:13.07136399","country":"Longitude:77.55943079","twitter":"Current Location"},{"name":"Latitude:13.07136399","country":"Longitude:77.55943079","twitter":"Current Location"},{"name":"Latitude:13.07136399","country":"Longitude:77.55943079","twitter":"Current Location"},{"name":"13.07136399","country":"77.55943079","twitter":"Current Location"},{"name":"13.07136399","country":"77.55943079","twitter":"Current Location"}]

Here is the android code.

这是android代码。

public class Cultural extends ListActivity {

    private ProgressDialog pDialog;


    private static String url = "http://hmkcode.appspot.com/jsonservlet";

    private static final String NAME = "name";
    private static final String COUNTRY = "country";
    private static final String TWITTER = "twitter";

    // contacts JSONArray
    JSONArray contacts = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cultural);

        contactList = new ArrayList<HashMap<String, String>>();

        ListView lv = getListView();

        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                // getting values from selected ListItem

                String name = ((TextView)view.findViewById(R.id.name)).getText().toString();

                String country = ((TextView) view.findViewById(R.id.time)).getText().toString();
                String twitter = ((TextView) view.findViewById(R.id.venue)).getText().toString();

                Intent in = new Intent(getApplicationContext(),SingleEventActivity.class);
                in.putExtra(NAME, name);
                in.putExtra(COUNTRY, country);
                in.putExtra(TWITTER, twitter);

                startActivity(in);

            }
        });

        // Calling async task to get json
        new GetContacts().execute();
    }

    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(Cultural.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    // contacts = jsonObj.getJSONArray(NAME);

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String name = c.getString(NAME);
                        String time = c.getString(COUNTRY);
                        String twitter=c.getString(TWITTER);

                        // Phone node is JSON Object

                        // tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value

                        contact.put(NAME, name);
                        contact.put(COUNTRY, time);
                        contact.put(TWITTER,twitter);

                        //contact.put(TAG_ADDRESS, address);

                        // adding contact to contact list
                        contactList.add(contact);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(Cultural.this, contactList,R.layout.list_item, new String[] { NAME, COUNTRY,TWITTER }, new int[] { R.id.name,R.id.time, R.id.venue});

            setListAdapter(adapter);
        }

    }

}

Whenever i try to run the app, i get a black screen and i get the following information in console: JSONArray cannot be converted to JSONObject error.

每当我尝试运行应用程序时,我得到一个黑屏,我在控制台中获得以下信息:JSONArray无法转换为JSONObject错误。

The data is getting downloaded and i am able to see it in console.

数据正在下载,我可以在控制台中看到它。

Where am i going wrong here ?

我在哪里错了?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是原来的你吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值