Reputation: 436
I'm trying to send the user_id
to PHP through JSON but it shows Error parsing data org.json.JSONException: Value 2 of type java.lang.Integer cannot be converted to JSONObject
.
How can i make this work? Thank you in advance.
Class :
public class Home1 extends Activity {
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter1 adapterrr;
SharedPreferences pref;
String uid;
String user_i,us;
String ques_i;
ArrayList<HashMap<String, String>> arraylist;
//static String BET_ID = "bet_id";
static String QUESTION = "question";
static String QUES_ID = "ques_id";
static String ANSWER = "answer";
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
Set<String> newset=new HashSet<String>();
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pref = PreferenceManager.getDefaultSharedPreferences(this);
setContentView(R.layout.questionlist1);
uid = pref.getString("user_id",null);
Log.d("uid", ""+uid);
Intent i = getIntent();
ques_i = i.getStringExtra("qsid");
Log.d("qsid", ""+ques_i);
new DownloadJSON().execute();
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
/*protected ArrayList<NameValuePair> parameters;
public DownloadJSON() {
parameters = new ArrayList<NameValuePair>();
NameValuePair us = new BasicNameValuePair("user_id", uid);
parameters.add(us);
}*/
@Override
protected Void doInBackground(Void... params) {
arraylist = new ArrayList<HashMap<String, String>>();
/*jsonobject = JSONfunctions
.getJSONfromURL("http://192.168.1.23/mutilatedphp/QuizGame/filtercheck.php");*/
try {
jsonobject = JSONfunctions
.getJSONfromURL("http://192.168.1.23/mutilatedphp/QuizGame/filtercheck.php?user_id="+uid);
jsonarray = jsonobject.getJSONArray("ques");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
map.put("ques_id", jsonobject.getString("ques_id"));
map.put("question", jsonobject.getString("question"));
map.put("answer", jsonobject.getString("answer"));
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
listview = (ListView) findViewById(R.id.listView2);
adapterrr = new ListViewAdapter1(Home1.this, arraylist);
listview.setAdapter(adapterrr);
}
Upvotes: 0
Views: 387
Reputation: 756
I think the problem is that you haven't check whether the attribute 'ques' exists.
When you use getJSONArray()
method on an non-exist attribute, JSON library will throw a exception and crash your app.
You can take a look at my little example, it checks the existence of 'ques' before doing anything further:
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class SimpleTest {
public static void main(String[] args) {
String notOk = "{\n" +
"\t\"success\":0,\n" +
"\t\"message\":\"No Categories found\"\n" +
"}";
String ok = "{\n" +
"\t\"ques\":\n" +
"\t\t[\n" +
"\t\t\t{\n" +
"\t\t\t\t\"ques_id\":\"3\",\n" +
"\t\t\t\t\"question\":\"What is the currency of Japan?\",\n" +
"\t\t\t\t\"answer\":\"Yen\"\n" +
"\t\t\t}\n" +
"\t\t],\n" +
"\t\"success\":1,\n" +
"\t\"message\":\"Successfully found \"\n" +
"}";
JSONObject okJSON = new JSONObject(ok);
JSONObject notOkJSON = new JSONObject(notOk);
System.out.println(new SimpleTest().parseJson(okJSON));
System.out.println(new SimpleTest().parseJson(notOkJSON));
}
public Map<String, String> parseJson(JSONObject jsonObject) {
if (jsonObject.has("ques")) {
JSONArray jsonArray = jsonObject.getJSONArray("ques");
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject tempObject = jsonArray.getJSONObject(i);
map.put("ques_id", tempObject.getString("ques_id"));
map.put("question", tempObject.getString("question"));
map.put("answer", tempObject.getString("answer"));
}
return map;
} else {
return new HashMap<String, String>();
}
}
}
Or, it is totally ok to use the 'success' attribute to check the result and tell you whether 'ques' attribute exists. Hope my answer can give you a little help.
Upvotes: 0
Reputation: 1684
The problem is caused by this line
jsonobject = jsonarray.getJSONObject(i);
You are trying to convert an object of type Integer into a JSONObject. The problem is on the server side. Please post your response, if you found the problem, so I can verify if I'm right here.
Upvotes: 1