Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

JSONArray cannot be converted to JSONObject?

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)

Answer*

Draft saved
Draft discarded
Cancel
1
  • It still doesn work. Iget the error Json object [] cannotbe converted. That means i doesnt even show the logged in data Commented Feb 25, 2014 at 12:07

default

AltStyle によって変換されたページ (->オリジナル) /