0

我正在开发一个 JSON 对象的应用程序,它将数据返回到 ListView。

在那个需要传递的参数中,它是用户的 Uid。

我的异步代码是:

class  AsyncCallWebServicereceiveHistory extends AsyncTask<String, String, String>
{
    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progressDialog = new ProgressDialog(ReceiveHistory.this);
        progressDialog.setTitle("Loading");
        progressDialog.setMessage("Please wait");
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(true);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... aurl)
    {
        Log.v("receiveHistory","Do in BG-1");
        uid=global.get_user_id();
        try
        {
            HttpPost postMethod = new HttpPost("http://demo1.idevtechnolabs.com/RChatAPI/receive_history.php");

            List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("uemail", uid));
            BufferedReader bufferedReader = null;

            HttpClient client = new DefaultHttpClient();
            HttpResponse response = null;

            response = client.execute(postMethod);
            final int statusCode = response.getStatusLine().getStatusCode();

            Log.v("Album ::","Response:::--->"+response.toString());
            Log.v("Album ::","Status Code:::--->"+statusCode);

            bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer stringBuffer = new StringBuffer("");
            String line = "";
            String LineSeparator = System.getProperty("line.separator");
            while ((line = bufferedReader.readLine()) != null)
            {
                stringBuffer.append(line + LineSeparator);
            }
            bufferedReader.close();

            //-------------CONVERT DATA TO JSON---------------------------------

            try
            {
                String myjsonstring = stringBuffer.toString();

                JSONArray jsonArray = new JSONArray(myjsonstring);

                JSONObject jsonObj = null;

                jsonObj = jsonArray.getJSONObject(0);
                code = jsonObj.getString("code");
                receiveMsgData.clear();

                Log.v("Home ::","Code:::--->"+code);

                code="0";
                if(code.equals("0"))
                {
                    for(int i=0; i<jsonArray.length();i++)
                    {

                            jsonObj = jsonArray.getJSONObject(i);
                            uname=jsonObj.getString("name");
                            uage = jsonObj.getString("age");
                            usex = jsonObj.getString("sex");
                            body = jsonObj.getString("country");
                            text= jsonObj.getString("text");

                            HashMap<String, String> tmp_album = new HashMap<String, String>();
                            tmp_album.put("receive_data", "Text");
                            Log.v("receive History","receive Data");
                            receiveMsgData.add(tmp_album);

                    }
                }
                catch (Exception e)
                {
                    Log.v("Home ::","Call JSON Exception in get Album in List--->"+e.toString());
                    e.printStackTrace();
                }
            }
            catch (Exception e)
            {
                Log.v("Exception: Get get Album in List","Name-"+e.toString());
                e.printStackTrace();
            }

            return code;
        }

        @Override
        protected void onPostExecute(String code)
        {
            if(code.equals("0"))
            {
                Receive_History_Custom_Adapter adapter = new Receive_History_Custom_Adapter(getApplicationContext(), receiveMsgData);
                lv.setAdapter(adapter);

            }
            else
            {
                Toast.makeText(getApplicationContext(), "Data not found", Toast.LENGTH_SHORT).show();
            }

            try
            {
                progressDialog.dismiss();
                progressDialog = null;
            }
            catch (Exception e)
            {
                // nothing
            }
        }
    }
}

我得到了以下异常:

Call JSON Exception in get Album in List--->org.json.JSONException: Value [] at 0 of type org.json.JSONArray cannot be converted to JSONObject

我的 Json 响应是:

[
    {
        "code":"0", "user_id":"21", "msg_id":"115", "name":"Sagar", "age":"18", "sex":"Male", "country":
        " India", "text":"hi", "photo":"demo.idevtechnolabs.com", "cnt":"1"
    },
    {
        "code":"0", "user_id":"18", "msg_id":"114", "name":"Ramani", "age":"20", "sex":"Male", "country":
        "Pakistan", "text":"hi", "photo":"demo.idevtechnolabs.com", "cnt":"1"
    }
]

谁能告诉我这是什么原因,我该如何解决这个问题。

提前致谢!

4

1 回答 1

0

JSONArray如果该项目的代码等于 ,我认为您正在尝试将每个项目添加到消息列表中0。如果是这样,你的代码是错误的,重写它比解释更容易:

try {
    String myjsonstring = stringBuffer.toString();
    JSONArray jsonArray = new JSONArray(myjsonstring);
    receiveMsgData.clear();
    for (int i = 0; i < jsonArray.length(); i++) {
        if (jsonArray.getJSONObject(i).getInt("code") == 0) {
            JSONObject data = jsonArray.getJSONObject(i);
            HashMap<String, String> tmp_album = new HashMap<String, String>();
            tmp_album.put("name", data.getString("name"));
            tmp_album.put("age", data.getString("age"));
            tmp_album.put("sex", data.getString("sex"));
            tmp_album.put("country", data.getString("country"));
            tmp_album.put("text", data.getString("text"));
            receiveMsgData.add(tmp_album);
        }
    }

} catch (Exception e) {
    Log.v("Home ::", "Call JSON Exception in get Album in List--->" + e.toString());
    e.printStackTrace();
}

在这里,我们遍历JSONArray并检查是否code等于0。如果相等,我们将必要的字段放入临时字段HashMap,然后将其附加到消息列表中。

于 2014-04-02T10:36:53.117 回答