Java 新闻应用程序RecyclerView未显示从API获取的数据
我一直在尝试使用newsapi.org上流行的新闻API构建这个新闻应用程序。我有个问题,我有点夹在中间。我面临的问题是,我的应用程序没有在recyclerView上显示从API获取的任何数据 在相同的情况下,如果我试图通过形成一些虚拟数组来显示数据,则recyclerview可以正常工作 这是我的MainActivity.java文件Java 新闻应用程序RecyclerView未显示从API获取的数据,java,android,android-recyclerview,android-volley,Java,Android,Android Recyclerview,Android Volley,我一直在尝试使用newsapi.org上流行的新闻API构建这个新闻应用程序。我有个问题,我有点夹在中间。我面临的问题是,我的应用程序没有在recyclerView上显示从API获取的任何数据 在相同的情况下,如果我试图通过形成一些虚拟数组来显示数据,则recyclerview可以正常工作 这是我的MainActivity.java文件 public class MainActivity extends AppCompatActivity implements RecyclerViewClick
public class MainActivity extends AppCompatActivity implements RecyclerViewClickInterface{
private RequestQueue mRequestQueue;
private ArrayList<NewsData> mNewsArray = new ArrayList<>();
private JSONArray newsJsonArray;
//private JSONObject mSingleJsonObject;
private NewsAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView listOfItems = (RecyclerView) findViewById(R.id.recyclerViewList);
listOfItems.setLayoutManager(new LinearLayoutManager(this));
mRequestQueue = VolleySingleton.getInstance(this).getRequestQueue();
mAdapter = new NewsAdapter(this);
fetchData();
listOfItems.setAdapter(mAdapter);
}
public void fetchData(){
String url = "https://newsapi.org/v2/top-headlines?country=in&apiKey=240f35xxxxx12bb70xxxxxx";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
newsJsonArray = response.getJSONArray("article");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i=0;i<newsJsonArray.length();i++){
try {
JSONObject mSingleJsonObject = newsJsonArray.getJSONObject(i);
NewsData mSingleNewsData = new NewsData(
mSingleJsonObject.getString("title"),
mSingleJsonObject.getString("author"),
mSingleJsonObject.getString("url"),
mSingleJsonObject.getString("urlToImage")
);
mNewsArray.add(mSingleNewsData);
} catch (JSONException e) {
e.printStackTrace();
}
} //for loop
mAdapter.updateNews(mNewsArray);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
VolleySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
}
@Override
public void onItemClicked(int position) {
//Toast.makeText(this, (CharSequence) data.get(position),Toast.LENGTH_SHORT).show();
}
}
请帮帮我:)
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newsapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您的api出现403错误,因此无法获取任何数据并在recyclerview中显示。您的api密钥无效,因此无法从api获取数据。还有一件事,你应该隐藏你的api密钥,同时将它发布到某个地方。我已经重新检查过了,而且在浏览器中打开的链接工作正常。请上传你的清单文件…@piyushpk上传了清单文件!当我在logcat中复制链接时,浏览器上会出现错误吗?
2020-11-09 01:09:57.206 13597-13645/com.example.newsapplication E/Volley: [2204] BasicNetwork.performRequest: Unexpected response code 403 for https://newsapi.org/v2/top-headlines?country=in&apiKey=240xxxxxx61xxxxf0e8fbf
2020-11-09 01:09:57.932 13597-13645/com.example.newsapplication E/Volley: [2204] BasicNetwork.performRequest: Unexpected response code 403 for https://newsapi.org/v2/top-headlines?country=in&apiKey=240xxxxxx61xxxxf0e8fbf
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newsapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>