2014-02-24 188 views
0

您好我收到此错误信息,每当我尝试连接到我的SQL数据库:JSONArray无法转换为JSONObject?

JSONArray cannot be converted to JSONObject

获取数据从数据库工作正常,但是当我试图让他的数据用户是谁登录在我得到上述错误。

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

} 

这里是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.access$600(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) 
+1

什么是JSON数据是什么样子? –

+0

请显示您的json格式 – nikis

+0

{“users”:[{“id”:“1”,“username”:“ertas”,“adres”:“street 62”}]} – user3271587

回答

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

我觉得这个代码抛出异常typeMismatch

试试这个:

int id = jsonChildNode.getInt("id"); 
String usrNm = jsonChildNode.getString("username"); 
String adres = jsonChildNode.getString("adres"); 
+0

之上添加了logcat它还没有工作。 Iget错误Json对象[]无法转换。这意味着我甚至不会显示登录的数据 – user3271587

相关问题