2011-07-16 60 views
0

你好我試圖填充使用數組從這裏http客戶端自定義列表視圖是一個代碼可能有人請給我什麼,我做錯了使用一個HashMap

package com.capefield.msemakweli; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.util.ArrayList; 
import java.util.HashMap; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.SimpleAdapter; 


public class viewComms extends ListActivity { 

    public String names[]; 
    public String commsFromDb[]; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.comment_list); 

     //create http client to gather comments 
     try{ 
     HttpClient getComment = new DefaultHttpClient(); 
     HttpGet pullComm = new HttpGet(); 
     pullComm.setURI(new URI("http://msemakweli.olalashe.com/getComments.php")); 
     HttpResponse comments = getComment.execute(pullComm); 
     if (comments.getStatusLine().getStatusCode() == 200) { 
      HttpEntity entit = comments.getEntity(); 
      if (entit !=null){ 
       InputStream commStream = entit.getContent(); 
       JSONArray commentAR = new JSONArray(
         convertStreamToString(commStream)); 
       if (commentAR.length()==0){ 

       }else{ 
        names = new String[commentAR.length()]; 
        commsFromDb = new String [commentAR.length()]; 

        for (int i = 0; i < commentAR.length(); i++) { 
         JSONObject OComm = commentAR.getJSONObject(i); 
         names[i] = OComm.getString("name"); 
         commsFromDb[i] = OComm.getString("comment"); 
        } 
        ArrayList<HashMap<String, String>> commList = new ArrayList<HashMap<String, String>>(); 
        for (int j=0; j<commentAR.length(); j++){ 
         HashMap<String, String> objComm = new HashMap<String, String>(); 
         objComm.put("name", names[j]); 
         objComm.put("suggestiom,", commsFromDb[j]); 
         commList.add(objComm); 

         SimpleAdapter adapter =new SimpleAdapter(
           this, 
           commList, 
           R.layout.comment_row, 
           new String[]{"name","suggestiom,"}, 
           new int[]{R.id.CommName, R.id.CommText} 
           ); 
         setListAdapter(adapter); 
        } 










       } 

      } 

     } 



     } catch (URISyntaxException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }finally{ 

     } 


    } 

    private static String convertStreamToString(InputStream is) { 
     /* 
     * To convert the InputStream to String we use the 
     * BufferedReader.readLine() method. We iterate until the BufferedReader 
     * return null which means there's no more data to read. Each line will 
     * appended to a StringBuilder and returned as String. 
     */ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 



} 
暗示填充在Android的定製多的ListView

這裏是堆棧跟蹤我得到當我運行活動

07-17 10:29:14.713: WARN/System.err(4936): org.json.JSONException: No value for comment 
07-17 10:29:14.723: WARN/System.err(4936):  at org.json.JSONObject.get(JSONObject.java:354) 
07-17 10:29:14.733: WARN/System.err(4936):  at org.json.JSONObject.getString(JSONObject.java:510) 
07-17 10:29:14.733: WARN/System.err(4936):  at com.capefield.msemakweli.viewComms.onCreate(viewComms.java:58) 
07-17 10:29:14.733: WARN/System.err(4936):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-17 10:29:14.733: WARN/System.err(4936):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
07-17 10:29:14.733: WARN/System.err(4936):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
07-17 10:29:14.743: WARN/System.err(4936):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
07-17 10:29:14.743: WARN/System.err(4936):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
07-17 10:29:14.743: WARN/System.err(4936):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-17 10:29:14.743: WARN/System.err(4936):  at android.os.Looper.loop(Looper.java:123) 
07-17 10:29:14.743: WARN/System.err(4936):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
07-17 10:29:14.743: WARN/System.err(4936):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-17 10:29:14.743: WARN/System.err(4936):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-17 10:29:14.753: WARN/System.err(4936):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
07-17 10:29:14.753: WARN/System.err(4936):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
07-17 10:29:14.753: WARN/System.err(4936):  at dalvik.system.NativeStart.main(Native Method) 

這裏是comment_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ListView 
    android:id="@+id/android:list" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"></ListView> 

</LinearLayout> 

三江源

回答

1
ArrayList<HashMap<String, String>> commList = new ArrayList<HashMap<String, String>>(); 
       for (int j=0; j<commentAR.length(); j++){ 
        HashMap<String, String> objComm = new HashMap<String, String>(); 
        objComm.put("name", names[j]); 
        objComm.put("suggestiom,", commsFromDb[j]); 
        commList.add(objComm); 
       } 

沒有理由使用整個HashMap實例只是一個鍵映射到一個值。改爲使用BasicNameValuePair或String []。

您的整個convertStringToStream()方法可以被BasicResponseHandler替換 - 2行代碼,實例化和處理響應。其實,你可以切出更多的與BasicResponseHandler代碼的..

http://developer.android.com/reference/org/apache/http/impl/client/BasicResponseHandler.html

儘管如此,如果你的代碼崩潰,這可能更多的可能不是修復它(獵槍調試術語)。如果您包含堆棧跟蹤,我們可以告訴您是什麼導致了故障。檢查show-> view-> android-> logcat

+0

嗨,對不起,我昨天沒有包含足夠的細節,我的代碼沒有崩潰,當我啓動這個活動的結果是,它給了我一個空白的黑暗佈局沒有更多 – zacck

+0

@zacck粘貼這個佈局xml setContentView(R.layout.comment_list); –

+0

我已經發布了comment_list非常感謝您的幫助 – zacck