• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps JSONArray cannot be converted to JSONObject?

ugur

Lurker
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.

[HIGH]
@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;
}

}
[/HIGH]

and this is the Logcat:

[HIGH]02-25 10:26:42.690 5233-5233/com.example.app W/System.err&#65109; at org.json.JSON.typeMismatch(JSON.java:111)
02-25 10:26:42.690 5233-5233/com.example.app W/System.err&#65109; at org.json.JSONObject.<init>(JSONObject.java:158)
02-25 10:26:42.690 5233-5233/com.example.app W/System.err&#65109; at org.json.JSONObject.<init>(JSONObject.java:171)
02-25 10:26:42.690 5233-5233/com.example.app W/System.err&#65109; at com.example.app.MoviesFragment.ListDrwaer(MoviesFragment.java:110)
02-25 10:26:42.690 5233-5233/com.example.app W/System.err&#65109; at com.example.app.MoviesFragment$JsonReadTask.onPostExecute(MoviesFragment.java:95)
02-25 10:26:42.690 5233-5233/com.example.app W/System.err&#65109; at com.example.app.MoviesFragment$JsonReadTask.onPostExecute(MoviesFragment.java:55)
02-25 10:26:42.690 5233-5233/com.example.app W/System.err&#65109; at android.os.AsyncTask.finish(AsyncTask.java:631)
02-25 10:26:42.690 5233-5233/com.example.app W/System.err&#65109; at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-25 10:26:42.700 5233-5233/com.example.app W/System.err&#65109; at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-25 10:26:42.700 5233-5233/com.example.app W/System.err&#65109; at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 10:26:42.700 5233-5233/com.example.app W/System.err&#65109; at android.os.Looper.loop(Looper.java:137)
02-25 10:26:42.700 5233-5233/com.example.app W/System.err&#65109; at android.app.ActivityThread.main(ActivityThread.java:4931)
02-25 10:26:42.700 5233-5233/com.example.app W/System.err&#65109; at java.lang.reflect.Method.invokeNative(Native Method)
02-25 10:26:42.700 5233-5233/com.example.app W/System.err&#65109; at java.lang.reflect.Method.invoke(Method.java:511)
02-25 10:26:42.700 5233-5233/com.example.app W/System.err&#65109; 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&#65109; at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-25 10:26:42.700 5233-5233/com.example.app W/System.err&#65109; at dalvik.system.NativeStart.main(Native Method)[/HIGH]
 
Try printing out the string before parsing it to see if its in the correct format (JSON).

Oh, code tags around code help others to easily read your code, i.e. it keeps the formatting such as indentation.
 
Back
Top Bottom