2014-02-28 8 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

을이 줄을 추가 애호가 y 여기에 게시하는 코드에서. 그것은 기술적으로 사적인 정보입니다. –

+0

조언을 위해서 문자열을 사용하십시오 stringEntity = EntityUtils.toString (httpEntity); 및 JSONArray json = 새 JSONArray (stringEntity); – Dadroid

답변

0

당신이 연결 시도 때문에 android.os.NetworkOnMainThreadException을 가지고해야합니다 가지고 있지만. onCreate() 방법에있는 주요 스레드에 네트워크에. 안드로이드 버전 +3.0 UI 스레드에 인터넷에 연결할 수 없으므로 Handlers 또는 AsyncTask을 인터넷에 연결해야합니다. 가장 좋은 방법은 AsyncTask 클래스입니다.

샘플 및 설명서는 다음 링크를 참조하십시오.

Document

AsyncTask Android example

당신은 더 많은 정보를 원하시면 다음 문서를 볼 수 있습니다 당신은 아마 당신의 포스 API 제거해야

Android Background Processing with Handlers and AsyncTask and Loaders - Tutorial

0

가 ListAdapter

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

'arrayAdapter'에서'constructor'의 super를 필요로하지 않습니다 .' BaseAdapter에서'getCount'와'getItem'을 override해야합니다. –

+0

로그에 대한 예외 보고서가 있습니까? –