[英]How to get java objects from JSONArray url using Jackson in Android
[英]How to request and get jsonarray from url and view them on listview in android
我是Android Studio的新手,我想創建一個RSS閱讀器應用程序。 應用程序應從url獲取最新新聞ID,將其與應用程序中現有的最新新聞ID進行比較,發送請求並獲取它們之間的每個ID的json數組。下載json完成后,將其顯示在列表視圖中。 問題是我不知道如何將它們添加到列表視圖。 MainActivity類是:
//sorry guys this class is totally messed up!
package com.example.sorush.slidr;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.example.sorush.slidr.model.NewsItems;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.r0adkll.deadskunk.adapters.BetterRecyclerAdapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
public class MainActivity extends ActionBarActivity {
@InjectView(R.id.recycler)
RecyclerView mRecycler;
private OSVersionAdapter mAdapter;
List<NewsItems> oss = null;
int i=1;
String lastidurl = api + "name=getnewsid";
String URL = api + "name=getnews" + i;
private final static String api = "api link";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
new DownloadFilesTask();
}
/**
* Intialize Recycler
*/
private void initRecycler(){
mAdapter = new OSVersionAdapter();
mAdapter.addAll(getJSONFromUrl(URL));
mRecycler.setAdapter(mAdapter);
mRecycler.setLayoutManager(new LinearLayoutManager(this));
mAdapter.setOnItemClickListener(new BetterRecyclerAdapter.OnItemClickListener<NewsItems>() {
@Override
public void onItemClick(View view, NewsItems androidOS, int i) {
// Launch the slidable activity
Intent viewer = new Intent(MainActivity.this, ViewerActivity.class);
viewer.putExtra(ViewerActivity.EXTRA_OS, androidOS);
startActivity(viewer);
}
});
}
private class DownloadFilesTask extends AsyncTask<String, Integer, Void> {
@Override
protected void onProgressUpdate(Integer... values) {
}
@Override
protected void onPostExecute(Void result) {
if (oss != null) {
initRecycler();
}
}
@Override
protected Void doInBackground(String... params) {
String lastid;
lastid = getLastNewsID(lastidurl);
int id = Integer.parseInt(lastid);
if(lastid!=null) {
for (i = 0; i < id ; i++) {
String url = params[i];
getJSONFromUrl(url);
}
}else{
//
}
if(i==lastid.length()){
Intent intent = new Intent(MainActivity.this, ViewerActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(MainActivity.this);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("News")
.setContentTitle("News Available")
.setContentText("news text")
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
return null;
}
}
public List<NewsItems> getJSONFromUrl(String url) {
InputStream is = null;
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
InputStreamReader isr = new InputStreamReader(is);
Gson gson = new Gson();
Type listType = new TypeToken<List<NewsItems>>(){}.getType();
oss = gson.fromJson(isr, listType);
is.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return oss;
}
public final static String getLastNewsID(String s){
InputStream is = null;
JSONObject jObj = null;
String id = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(s);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
id = sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return id;
}
}
和NewsItems類:
package com.example.sorush.slidr.model;
import android.os.Parcel;
import android.os.Parcelable;
public class NewsItems implements Parcelable{
public String author;
public String newstitle;
public int newsid;
public String thumb;
public String date;
public String content;
public NewsItems(){}
private NewsItems(Parcel in){
author = in.readString();
newstitle = in.readString();
newsid = in.readInt();
thumb = in.readString();
date = in.readString();
content = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(author);
dest.writeString(newstitle);
dest.writeInt(newsid);
dest.writeString(thumb);
dest.writeString(date);
dest.writeString(content);
}
public static final Creator<NewsItems> CREATOR = new Creator<NewsItems>() {
@Override
public NewsItems createFromParcel(Parcel source) {
return new NewsItems(source);
}
@Override
public NewsItems[] newArray(int size) {
return new NewsItems[size];
}
};
}
由於您將NewsItems保留在列表中,因此我建議使用ArrayAdapter將數據與ListView綁定。 這是我使用的適配器的示例。
public class ResultAdapter extends ArrayAdapter<Result> {
private List<Result> items;
private Context context;
static class ViewHolder {
public ImageView icon;
public TextView title;
public TextView type;
}
public ResultAdapter(Context context, int resource, List<Result> objects) {
super(context, resource, objects);
this.context = context;
items = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder pattern - view stores its subviews when it's created. Next time the view is looked up it
// reuses the views (no need to search for those again)
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.result_item, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.icon = (ImageView) convertView.findViewById(R.id.result_item_icon);
viewHolder.title = (TextView) convertView.findViewById(R.id.result_item_title);
viewHolder.type = (TextView) convertView.findViewById(R.id.result_item_type);
convertView.setTag(viewHolder);
}
// now the ViewHolder is prepared, next get its tag and prepare the view
ViewHolder holder = (ViewHolder) convertView.getTag();
Result result = items.get(position);
holder.title.setText(result.text);
// the Utils class is my own helper function that get me what I need from config
holder.type.setText(Utils.getTypeString(context, result.type, true));
holder.icon.setImageResource(Utils.getTypeSearchDrawable(result.type));
holder.title.setTextColor(Utils.getTypeColor(context, result.type));
return convertView;
} }
然后在您的活動中,將ListView的適配器設置為所需的適配器:
ArrayList<Result> results = new ArrayList<Result>();
// fill the ArrayList results with data - you would probably get it already filled from your AsyncTask
results.add(...);
ListView list = (ListView) findViewById(R.id.result_list_view);
list.setAdapter(new ResultAdapter(this, R.id.list_item, results));
一些注意事項:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.