0

Hi I get this error message whenever I try to connect to my sql database:

JSONArray cannot be converted to JSONObject

Getting the data from the database works fine, but when i try to get the data of he user who is logged in i get the above error.

@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;
}
}

And here is the 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.access600ドル(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)
NoGall
5,2366 gold badges42 silver badges72 bronze badges
asked Feb 24, 2014 at 14:14
7
  • 1
    What does the JSON data look like? Commented Feb 24, 2014 at 14:16
  • please show your json format Commented Feb 24, 2014 at 14:17
  • {"users":[{"id":"1","username":"ertas","adres":"street 62"}]} Commented Feb 24, 2014 at 14:19
  • is this failing before the for loop in ListDrwaer() or in it? Commented Feb 24, 2014 at 14:31
  • When I open the fragment I get the error immediately, but the app doesnt crash I just get an empty page.When I just select * from users I dont get an error , but when I`m trying to retrieve those data through a session i get this error Commented Feb 24, 2014 at 14:44

1 Answer 1

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

I think this code throws exception typeMismatch.

Try this:

int id = jsonChildNode.getInt("id");
String usrNm = jsonChildNode.getString("username");
String adres = jsonChildNode.getString("adres");
answered Feb 25, 2014 at 9:38
Sign up to request clarification or add additional context in comments.

1 Comment

It still doesn work. Iget the error Json object [] cannotbe converted. That means i doesnt even show the logged in data

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.