public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<BookData>> {
private static final String GOOGLEBOOKURL = "https://www.googleapis.com/books/v1/volumes?q=search+terms";
private static final String TAG = "Mainactivity";
private RecyclerView recyclerview;
private RecyclerAdapter recyclerAdapter;
private static final int BOOK_LOADER_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<BookData> arrayList = new ArrayList<>();
recyclerAdapter = new RecyclerAdapter(this,arrayList);
recyclerview = (RecyclerView)findViewById(R.id.recyclerView);
recyclerview.setLayoutManager(new LinearLayoutManager(this));
recyclerview.setAdapter(recyclerAdapter);
LoaderManager loaderManager = getSupportLoaderManager();
Log.i(TAG, "Now calling initLoader");
loaderManager.initLoader(BOOK_LOADER_ID, null, this);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
recyclerview.addItemDecoration(dividerItemDecoration);
}
@NonNull
@Override
public Loader<List<BookData>> onCreateLoader(int id, @Nullable Bundle args) {
Log.i(TAG,"Problem with onCreateloader");
return new BookLoader(MainActivity.this,GOOGLEBOOKURL);
}
@Override
public void onLoadFinished(@NonNull Loader<List<BookData>> loader, List<BookData> data) {
recyclerAdapter.clear();
if (data != null && !data.isEmpty()) {
recyclerAdapter.addAll(data);
}
}
@Override
public void onLoaderReset(@NonNull Loader<List<BookData>> loader) {
recyclerAdapter.clear();
Log.i(TAG, "Now loader is resetting");
}
}
适配器
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
ArrayList<BookData> mArrayList;
Context mContext;
public RecyclerAdapter(Context context, ArrayList<BookData> arrayList) {
mArrayList = arrayList;
mContext = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ViewHolder viewHolder;
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View v = layoutInflater.inflate(R.layout.rowrepresent,parent,false);
viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
BookData currentPosition = mArrayList.get(position);
// holder.authorname.setText(currentPosition.getAuthorName());
holder.titlename.setText(currentPosition.getTitleName());
// holder.mImageView.setImageResource(Integer.parseInt(currentPosition.getImage()));
}
@Override
public int getItemCount() {
return mArrayList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
ImageView mImageView;
TextView titlename,authorname;
public ViewHolder(@NonNull View itemView) {
super(itemView);
// mImageView = (ImageView)itemView.findViewById(R.id.bookImage);
titlename=(TextView)itemView.findViewById(R.id.titleName);
// authorname=(TextView)itemView.findViewById(R.id.authorName);
}
}
public void clear() {
if (mArrayList != null && !mArrayList.isEmpty()) {
int size = mArrayList.size();
mArrayList.clear();
notifyItemRangeRemoved(0, size);
}
}
public void addAll(List<BookData> data){
mArrayList.addAll(data);
}
}
网络
public class Network {
private static final String TAG = "Network";
public static ArrayList<BookData> extractbooksfromurl(String requesturl){
ArrayList<BookData> BookDetails;
URL url = createUrl(requesturl);
String jsonresponse = null;
try {
jsonresponse = makeHttprequest(url);
} catch (IOException e) {
Log.e(TAG, "Error closing input stream", e);
}
BookDetails = extractInfojson(jsonresponse);
return BookDetails;
}
private static URL createUrl(String requestUrl) {
URL url = null;
try {
url = new URL(requestUrl);
} catch (MalformedURLException e) {
Log.e(TAG, "The problem with URL", e);
}
return url;
}
private static String makeHttprequest(URL url) throws IOException {
String jsonresponse = "";
if (url == null) {
return jsonresponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(1000);
urlConnection.setReadTimeout(1500);
urlConnection.connect();
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonresponse = readfromstream(inputStream);
} else {
Log.e(TAG, "Error response code:" + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(TAG, "problem with MakeHttprequest method", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonresponse;
}
private static String readfromstream(InputStream inputStream)throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream!= null){
InputStreamReader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader bufferedReader = new BufferedReader(reader);
try {
String line = bufferedReader.readLine();
while (line!=null ){
output.append(line);
line = bufferedReader.readLine();
}
} catch (IOException e) {
Log.e(TAG,"Problem in readfromstream method",e);
}
} return output.toString();
}
public static ArrayList<BookData> extractInfojson(String bookListData){
if (TextUtils.isEmpty(bookListData)){
return null;
}
ArrayList<BookData> arrayListnew = new ArrayList<>();
String authorsName = "";
String title;
String images;
try {
JSONObject jsonRootObject = new JSONObject(bookListData);
JSONArray jsonArray = jsonRootObject.optJSONArray("item");
if (jsonArray != null) {
for (int i = 0; i<jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.optJSONObject(i);
JSONObject volumeinfo = jsonObject.getJSONObject("volumeInfo");
/* JSONArray authors_list ;
if (volumeinfo.has("authors")){
authors_list = volumeinfo.getJSONArray("authors");
for (int y=0;y<authors_list.length();y++)
authorsName = authors_list.getString(y);
}else{
authorsName ="No Author";
}
title = volumeinfo.getString("title").toString();
JSONObject image = volumeinfo.getJSONObject("imageLinks");
images = image.getString("thumbnail").toString();*/
title = volumeinfo.getString("title").toString();
arrayListnew.add(new BookData(title));
}
}
} catch (JSONException e) {
Log.e(TAG,"problem in parsing the data",e);
}
return arrayListnew;
}
}
logcats公司
2020-11-21 11:27:29.407 12457-12457/com.example.booklistapp I/Mainactivity: Now calling initLoader
2020-11-21 11:27:29.407 12457-12457/com.example.booklistapp I/Mainactivity: Problem with onCreateloader
2020-11-21 11:27:29.412 12457-12457/com.example.booklistapp I/BookLoader: Now onStartLoading method
2020-11-21 11:27:29.417 12457-12472/com.example.booklistapp I/BookLoader: Now LoadinBackground method
2020-11-21 11:27:29.426 12457-12472/com.example.booklistapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-11-21 11:27:29.496 12457-12473/com.example.booklistapp I/OpenGLRenderer: Initialized EGL, version 1.4
2020-11-21 11:27:29.496 12457-12473/com.example.booklistapp D/OpenGLRenderer: Swap behavior 1
2020-11-21 11:27:29.496 12457-12473/com.example.booklistapp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2020-11-21 11:27:29.496 12457-12473/com.example.booklistapp D/OpenGLRenderer: Swap behavior 0
2020-11-21 11:27:29.575 12457-12473/com.example.booklistapp D/EGL_emulation: eglCreateContext: 0x9b827520: maj 2 min 0 rcv 2
2020-11-21 11:27:29.688 12457-12473/com.example.booklistapp D/EGL_emulation: eglMakeCurrent: 0x9b827520: ver 2 0 (tinfo 0x9b89ad10)
2020-11-21 11:27:29.733 12457-12473/com.example.booklistapp D/EGL_emulation: eglMakeCurrent: 0x9b827520: ver 2 0 (tinfo 0x9b89ad10)
有人帮我在这一个,我没有得到任何错误,但仍然没有答案显示?为什么?谢谢您。
1条答案
按热度按时间sirbozc51#
在你的
Recyclerview
适配器addAll
方法,在添加到数组后调用notifydatasetchanged()这样地:
更新:
根据注解,当您使用虚拟数据时,代码可以工作,这指向网络请求逻辑中的另一个问题。您可以通过在方法或代码行中放置各种断点来调试应用程序,以帮助确定代码逻辑未按预期运行的位置。https://developer.android.com/studio/debug 任何其他问题你应该在一个新的问题,因为这一个是真正解决。
我还建议使用像volley这样的库,这将大大简化您的网络请求。具体见截击中的标准请求