올바른 방식으로 데이터를 구문 분석했지만 표시 할 수 없다고 생각했습니다. 제 실수가 어디 있는지 알려주십시오.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 단지에서 내 목록보기
을이 줄을 추가 애호가 y 여기에 게시하는 코드에서. 그것은 기술적으로 사적인 정보입니다. –
조언을 위해서 문자열을 사용하십시오 stringEntity = EntityUtils.toString (httpEntity); 및 JSONArray json = 새 JSONArray (stringEntity); – Dadroid