JSON解析数据未显示

时间:2014-02-28 18:18:00

标签: android json parsing listview android-arrayadapter

我以为我以正确的方式解析了我的数据,但我无法显示它。你能告诉我我的错误在哪里吗?

我有各种各样的东西正在传递,但我只希望列表视图中的每个项目都显示某些属性,以下是我的代码。我只是出现了一个空白的主要活动。

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只有一个列表视图。

2 个答案:

答案 0 :(得分:0)

你必须得到 android.os.NetworkOnMainThreadException ,因为你试图在onCreate()方法的主线程上连接到网络。由于Android版+3.0无法通过UI线程连接到互联网,您必须使用HandlersAsyncTask连接到互联网,最好的方法是 AsyncTask

有关示例和文档,请参阅以下链接。

Document

AsyncTask Android example

您可以在以下文章中查看更多信息:

Android Background Processing with Handlers and AsyncTask and Loaders - Tutorial

答案 1 :(得分:0)

在ListAdapter上添加此行

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