我目前正在为Android构建新闻应用程序 我正在使用News API来获取运行良好的数据源,但是只有一个新闻源可以添加多个新闻源吗?
这是我的java主要活动
public class MainActivity extends AppCompatActivity {
String API_KEY = "8190df9eb51445228e397e4185311a66";
String NEWS_SOURCE = "bbc-news";
ListView listNews;
ProgressBar loader;
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
static final String KEY_AUTHOR = "author";
static final String KEY_TITLE = "title";
static final String KEY_DESCRIPTION = "description";
static final String KEY_URL = "url";
static final String KEY_URLTOIMAGE = "urlToImage";
static final String KEY_PUBLISHEDAT = "publishedAt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listNews = (ListView) findViewById(R.id.listNews);
loader = (ProgressBar) findViewById(R.id.loader);
listNews.setEmptyView(loader);
if(Function.isNetworkAvailable(getApplicationContext()))
{
DownloadNews newsTask = new DownloadNews();
newsTask.execute();
}else{
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
}
}
class DownloadNews extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
String xml = "";
String urlParameters = "";
xml = Function.excuteGet("https://newsapi.org/v1/articles?source="+NEWS_SOURCE+"&sortBy=top&apiKey="+API_KEY, urlParameters);
return xml;
}
@Override
protected void onPostExecute(String xml) {
if(xml.length()>10){ // Just checking if not empty
try {
JSONObject jsonResponse = new JSONObject(xml);
JSONArray jsonArray = jsonResponse.optJSONArray("articles");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_AUTHOR, jsonObject.optString(KEY_AUTHOR).toString());
map.put(KEY_TITLE, jsonObject.optString(KEY_TITLE).toString());
map.put(KEY_DESCRIPTION, jsonObject.optString(KEY_DESCRIPTION).toString());
map.put(KEY_URL, jsonObject.optString(KEY_URL).toString());
map.put(KEY_URLTOIMAGE, jsonObject.optString(KEY_URLTOIMAGE).toString());
map.put(KEY_PUBLISHEDAT, jsonObject.optString(KEY_PUBLISHEDAT).toString());
dataList.add(map);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Unexpected error", Toast.LENGTH_SHORT).show();
}
ListNewsAdapter adapter = new ListNewsAdapter(MainActivity.this, dataList);
listNews.setAdapter(adapter);
listNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(MainActivity.this, DetailsActivity.class);
i.putExtra("url", dataList.get(+position).get(KEY_URL));
startActivity(i);
}
});
}else{
Toast.makeText(getApplicationContext(), "No news found", Toast.LENGTH_SHORT).show();
}
}
}
我试着查看他们网站上的文档https://newsapi.org/ 我无法看到任何我需要的公平性这可能是我试过的最复杂的任务,任何帮助将不胜感激。
我确信我已经忽视了一些事情,但绝大多数事情对我来说都很新鲜