2013-01-02 58 views
0

我使用異步任務與互聯網使用HTTPPost ... 這裏交互的Java代碼錯誤響應字符串轉換成JSON陣列 - 安卓

private void viewMyprofile(String username) { 
    System.out.println("in view my profile"); 
    class HttpGetAsyncTask extends AsyncTask<String, Void, String>{ 
     @Override 
     protected String doInBackground(String... params) { 
      String lUsername = params[0]; 

      HttpClient client = new DefaultHttpClient(); 
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
      HttpResponse response; 
      // JSONObject json = new JSONObject(); 
      System.out.println("in do in background"); 
      try{ 
       HttpPost post = new HttpPost(URL); 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
       nameValuePairs.add(new BasicNameValuePair("username", lUsername)); 
       nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi")); 
       post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       response = client.execute(post); 
       /*Checking response */ 
       if(response!=null){ 
        InputStream in = response.getEntity().getContent(); //Get the data in the entity 
        jsonResponse = convertStreamToString(in); 
        System.out.println("this is my response = " + jsonResponse); 
        return jsonResponse; 
       } 
      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 
      return null ; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 

      try { 
       JSONArray jsonArray = new JSONArray(result); 
       Log.i(MyProfile.class.getName(), 
        "Number of entries " + jsonArray.length()); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        Log.i(MyProfile.class.getName(), jsonObject.getString("password")); 
        textView.setText(jsonObject.getString("password")); 
       } 
       } catch (Exception e) { 
       e.printStackTrace(); 
       } 
     } 
    } 

    HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask(); 
    // Parameter we pass in the execute() method is relate to the first generic type of the AsyncTask 
    // We are passing the connectWithHttpGet() method arguments to that 
    httpGetAsyncTask.execute(username); 
    System.out.println("after execution call"); 
} 

public static String convertStreamToString(InputStream is) throws Exception { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 

    while ((line = reader.readLine()) != null) { 
     sb.append(line); 
    } 

    is.close(); 

    return sb.toString(); 
} 

}

這是兩個logcat的線...

01-02 08:51:31.789: I/System.out(21879): this is my response = {"0":"1","id":"1","1":"205041316950ae6ca20e6ac8.73557365","userid":"205041316950ae6ca20e6ac8.73557365","2":"Moderators","name":"Moderators","3":"ADMIN","username":"ADMIN","4":"umairisback","password":"umairisback","5":"[email protected]","email":"[email protected]","6":"1234567890","phone":"1234567890","7":"2012","batch":"2012","8":"","stream":"","9":"15\/08\/1994","dob":"15\/08\/1994","10":"on the PC!!! \\m\/","address":"on the PC!!! \\m\/","11":"Delhi Technological University","college":"Delhi Technological University","12":"Web Developer at LudlowCastle.co.in","occupation":"Web Developer at LudlowCastle.co.in","13":"School tym was totally awesome...\r\n\r\n Still miss it a lot ... :) ....","memories":"School tym was totally awesome...\r\n\r\n Still miss it a lot ... :) ...."} 
01-02 08:51:31.809: W/System.err(21879): org.json.JSONException: Value {"stream":"","phone":"1234567890","college":"Delhi Technological University","userid":"205041316950ae6ca20e6ac8.73557365","13":"School tym was totally awesome...\r\n\r\n Still miss it a lot ... :) ....","password":"umairisback","11":"Delhi Technological University","12":"Web Developer at LudlowCastle.co.in","id":"1","username":"ADMIN","name":"Moderators","occupation":"Web Developer at LudlowCastle.co.in","3":"ADMIN","2":"Moderators","10":"on the PC!!! \\m\/","1":"205041316950ae6ca20e6ac8.73557365","0":"1","7":"2012","address":"on the PC!!! \\m\/","6":"1234567890","email":"[email protected]","batch":"2012","5":"[email protected]","dob":"15\/08\/1994","4":"umairisback","9":"15\/08\/1994","memories":"School tym was totally awesome...\r\n\r\n Still miss it a lot ... :) ....","8":""} of type org.json.JSONObject cannot be converted to JSONArray 

就像你看到的我收到錯誤的響應字符串轉換成JSON數組... 請幫助我...

P.S.我是一個新手...

回答

1

根據你的輸出日誌,你沒有得到一個數組,你會得到一個JSONObject。所以才......

try { 
    JSONObject jsonObject = new JSONObject(result); 
    Log.i(MyProfile.class.getName(), jsonObject.getString("password")); 
    textView.setText(jsonObject.getString("password")); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

應該登錄並設置的TextView的文本umairisback

+1

附:爲了將來的參考,{}圍繞JSON對象,[]環繞JSON數組 –

+0

它工作。 thanx .. – omerjerk