我正在尝试从网络服务器检索数据并在List
Android
中显示,似乎我正在接收数据,但它从未出现在列表中,我与适配器混淆,我不知道它是否真的在做我想做的事情,虽然我从JSON
数据中收到某种类型的错误,它说类型不匹配,可能是这个原因?
以下是我称之为适配器的文件
public class MyKinstFragment extends Fragment {
private ListView listView ;
private Button newsButton, documentsButton;
private SimpleAdapter adpt;
//ArrayList<String> listItems= new ArrayList<String>();
/*private static String[] MOBILE_MODELS = {"iPhone 6","Nexus 6","Moto G","HTC One","Galaxy S5","Sony Xperia Z2","Lumia 830","Galaxy Grand 2"};*/
private static String[] MOBILE_MODELS = {"cachampo","champs","champi","champu","Galaxy S5","Sony Xperia Z2","Lumia 830","Galaxy Grand 2"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mykinst_layout, container, false);
initWidgets(rootView);
return rootView;
}
public void initWidgets(View rootView){
adpt = new SimpleAdapter(new ArrayList<News>(), rootView.getContext());
listView = (ListView) rootView.findViewById(R.id.listView);
listView.setAdapter(adpt);
listView.setClickable(true);
newsButton = (Button) rootView.findViewById(R.id.newsButton);
newsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newsButton.setBackgroundColor(Color.RED);
documentsButton.setBackgroundColor(Color.BLUE);
(new AsyncListViewLoader()).execute("http://kinst.gizmomen.com/api/v1/feeds/");
}
});
}
//GET news from webserver
private class AsyncListViewLoader extends AsyncTask<String, Void, List<News>> {
private final ProgressDialog dialog = new ProgressDialog(getActivity());
@Override
protected void onPostExecute(List<News> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
adpt.setItemList(result);
adpt.notifyDataSetChanged();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setMessage("Getting Data");
dialog.show();
}
@Override
protected List<News> doInBackground(String... params) {
// TODO Auto-generated method stub
List<News> result = new ArrayList<News>();
try {
URL u = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod("GET");
conn.connect();
InputStream is = conn.getInputStream();
//read
byte[] b = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (is.read(b) != -1)
baos.write(b);
String JSONResp = new String(baos.toByteArray());
JSONArray arr = new JSONArray(JSONResp);
for(int i=0; i < arr.length(); i++) {
result.add(convertNews(arr.getJSONObject(i)));
}
return result;
}
catch(Throwable t){
t.printStackTrace();
}
return null;
}
private News convertNews(JSONObject obj) throws JSONException {
String id = obj.getString("id");
String title = obj.getString("title");
String message = obj.getString("message");
String imageUrl = obj.getString("image_url");
String location = obj.getString("location");
return new News(id, title, message, imageUrl, location);
}
}
}
这是我为对象“新闻”定制的适配器
public class SimpleAdapter extends ArrayAdapter<News> {
private List<News> newsList;
private Context context;
public SimpleAdapter(List<News> newsList, Context ctx) {
super(ctx, android.R.layout.simple_list_item_1, newsList);
this.newsList = newsList;
this.context = ctx;
// TODO Auto-generated constructor stub
}
public int getCount() {
if(newsList != null)
return newsList.size();
return 0;
}
public News getItem(int position) {
if (newsList != null)
return newsList.get(position);
return null;
}
public long getItemId(int position) {
if (newsList != null)
return newsList.get(position).hashCode();
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if(v==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
News n = newsList.get(position);
TextView tle = (TextView) v.findViewById(R.id.title);
tle.setText(n.getTitle());
TextView msg = (TextView) v.findViewById(R.id.message);
msg.setText(n.getMessage());
TextView loc = (TextView) v.findViewById(R.id.location);
loc.setText(n.getLocation());
TextView iUrl = (TextView) v.findViewById(R.id.imgurl);
iUrl.setText(n.getImgURL());
return v;
}
public List<News> getItemList() {
return newsList;
}
public void setItemList(List<News> itemList) {
this.newsList = itemList;
}
}
我得到的原木猫是这样的:
05-20 21:47:02.690:W / System.err(741):org.json.JSONException:Value {“results”:[{“message”:“
just it&lt; / p&gt;“,”“id”:10,“title”:“new 4”,“location”:[87,89,90,91,92,94],“image_url”:“feed_images / Screen_Shot_2015-05-15_at_4 .13.47_pm_Rv9LCka.png“},{”message“:”
任何内容&lt; / p&gt;
第二行&lt; / p&gt;
结束&lt; / p&gt;“,”“id”:9,“title”:“第三个”,“位置”:[87,89,90,91,92,94],“image_url”:“feed_images / Screen_Shot_2015-04-28_at_4 .40.20_pm.png“},{”message“:”
带图像&lt; / p&gt;“,”id“:8,”title“:”另一个“,”位置“:[ 87,89,90,91,92,94],“image_url”:“feed_images / Screen_Shot_2015-04-28_at_4.35.51_pm.png”},{“message”:“
new with image&lt; / p&gt;
好吗?&lt; / p&gt;“,”id“:7,”title“:”with image“,”location“:[87,89,90,91,92,94 ],“image_url”:“feed_images / Screen_Shot_2015-05-15_at_4.13.47_pm.png”},“previous”:null,“count”:4,类型为org.json.JSONObject的“next”:null}不能转换为JSONArray 05-20 21:47:02.690:W / System.err(741):at org.json.JSON.typeMismatch(JSON.java:111) 05-20 21:47:02.690:W / System.err(741):at org.json.JSONArray。(JSONArray.java:91) 05-20 21:47:02.730:W / System.err(741):at org.json.JSONArray。(JSONArray.java:103) 05-20 21:47:02.760:W / System.err(741):at com.javacodegeeks.android.tablayout_with_fragments.MyKinstFragment $ AsyncListViewLoader.doInBackground(MyKinstFragment.java:139) 05-20 21:47:02.760:W / System.err(741):at com.javacodegeeks.android.tablayout_with_fragments.MyKinstFragment $ AsyncListViewLoader.doInBackground(MyKinstFragment.java:1) 05-20 21:47:02.830:W / System.err(741):在android.os.AsyncTask $ 2.call(AsyncTask.java:287) 05-20 21:47:02.830:W / System.err(741):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) 05-20 21:47:02.830:W / System.err(741):at java.util.concurrent.FutureTask.run(FutureTask.java:137) 05-20 21:47:02.830:W / System.err(741):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 05-20 21:47:02.830:W / System.err(741):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 05-20 21:47:02.830:W / System.err(741):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) 05-20 21:47:02.878:W / System.err(741):at java.lang.Thread.run(Thread.java:856)
但是我不认为JSON数据是问题,请帮助我永远欣赏它