我正在开发一个JSON对象的应用程序,它将数据返回到ListView。
在那个参数中需要传递的是Uid of User。
我的异步代码是:
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
[
{
"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"
}
]
任何人都可以告诉我这是什么原因以及如何解决这个问题。
提前致谢!
答案 0 :(得分: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
,然后将其附加到消息列表。