2014-02-28 82 views
0

我想我以正確的方式解析我的數據,但我無法顯示它。請讓我知道我的錯誤在哪裏?JSON解析數據不顯示

我有各種事情正​​在通過,但我只希望列表視圖上的每個項目顯示某些屬性,以下是我的代碼。我只是有一個空白的主要活動出現。

public class MainActivity extends Activity { 

List<Product> productList = new ArrayList<Product>(); 

ListAdapter adapter = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    String result = ""; 

    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     InputStream is = httpEntity.getContent(); 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 
     } catch (Exception e) { 
      Log.e("log_tag", "Error converting result" + e.toString()); 
     } 
    } catch (Exception e) { 
     Log.e("log_tag", "Error in http connection" + e.toString()); 
    } 

    try { 
     JSONArray json = new JSONArray(result); 
     for (int i = 0; i < json.length(); i++) { 
      JSONObject productsMade = json.getJSONObject(i); 

      int PRODUCT_ID = productsMade.getInt("productId"); 
      int STYLE_ID = productsMade.getInt("styleId"); 
      String BRAND_NAME = productsMade.getString("brandName"); 
      String PRODUCT_NAME = productsMade.getString("productName"); 
      int COLOR_ID = productsMade.getInt("colorId"); 
      int ORIGINAL_PRICE = productsMade.getInt("originalPrice"); 
      int PERCENT_OFF = productsMade.getInt("percentOff"); 
      int PRICE = productsMade.getInt("price"); 
      String PRODUCT_URL = productsMade.getString("productUrl"); 
      String IMAGE_URL = productsMade.getString("imageUrl"); 

      productList.add(new Product(PRODUCT_ID, STYLE_ID, BRAND_NAME, PRODUCT_NAME, 
        COLOR_ID, ORIGINAL_PRICE, PERCENT_OFF, PRICE, PRODUCT_URL, IMAGE_URL)); 
     } 
    } catch (JSONException e){ 
     Log.e("log_tag", "Error converting result" + e.toString()); 
    } 


    ListView myListView = (ListView)findViewById(R.id.list_view); 

    adapter = new ListAdapter(); 

    myListView.setAdapter(adapter); 

class ListAdapter extends ArrayAdapter<Product>{ 

    ListAdapter() { 
     super(MainActivity.this, android.R.layout.simple_list_item_1, productList); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 

     if (convertView == null) { 
      LayoutInflater inflater = getLayoutInflater(); 
      convertView = inflater.inflate(R.layout.list_values, null); 

      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 
     } 
     else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.populateFrom(productList.get(position)); 
     return (convertView); 
    } 

} 

class ViewHolder { 
    public TextView textBrandName = null; 
    public TextView textProductName = null; 
    public TextView textOriginalPrice = null; 


    ViewHolder(View row) { 
     textBrandName = (TextView)row.findViewById(R.id.brand_name); 
     textProductName = (TextView)row.findViewById(R.id.product_name); 
     textOriginalPrice = (TextView)row.findViewById(R.id.original_price); 
    } 

    void populateFrom(Product r) { 
     textBrandName.setText(r.getBrandName()); 
     textProductName.setText(r.getProductName()); 
     textOriginalPrice.setText(r.getOriginalPrice()); 
    } 
    } 

} 

我在哪裏錯了,我只是看到一個空白頁?

我list_value.xml文件看起來像:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<TextView 
    android:id="@+id/brand_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
/> 

<TextView 
    android:id="@+id/product_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
/> 

<TextView 
    android:id = "@+id/original_price" 
    android:layout_width = "match_parent" 
    android:layout_height = "wrap_content"/> 
</LinearLayout> 

雖然我main_activity.xml只是在內部列表視圖

+0

你或許應該刪除您Zappos的API柯你在這裏發佈的代碼。這是技術上的私人信息。 –

+0

只是建議,請使用String stringEntity = EntityUtils.toString(httpEntity);和JSONArray json = new JSONArray(stringEntity); – Dadroid

回答

0

必須得到android.os.NetworkOnMainThreadException因爲你嘗試連接。在onCreate()方法的主線程上聯網。由於android版本+3.0不能連接到互聯網上,因此您必須使用HandlersAsyncTask連接到互聯網,最好的方法是AsyncTask class。

查看示例和文檔,請參閱以下鏈接。

Document

AsyncTask Android example

你可以看到下面的文章的詳細信息:

Android Background Processing with Handlers and AsyncTask and Loaders - Tutorial

0

添加此行ListAdapter

@Overide 
public int getCount() 
{ 
return productList.size(); 
} 
+0

不需要這個,在'arrayAdapter'中,'super constructor'中做這個,'getCount'和'getItem'必須在BaseAdapter上覆蓋 –

+0

是否有任何關於日誌的異常報告? –