我写了一个包含recyclerview的片段,用于显示来自Internet的数据。但是,我发现getJSON
方法中的public class FragmentHot extends Fragment {
private Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
refreshRecyclerView();
break;
}
}
};
private View mView;
private List<Book> mBookList = new ArrayList<>();
BookRecyclerViewAdapter mBookRecyclerViewAdapter;
private void refreshRecyclerView() {
mBookRecyclerViewAdapter.notifyItemRangeInserted(0, mBookList.size() - 1);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle
savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_hot, container, false);
initRecyclerView();
return mView;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getJSON();
}
private void getJSON() {
new Thread(new Runnable() {
@Override
public void run() {
URL url;
InputStream inputStream = null;
try {
url = new URL("http://api.zhuishushenqi.com/cats/lv2/statistics");
inputStream = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject jsonObject = new JSONObject(builder.toString());
JSONArray books = jsonObject.getJSONArray("books");
File externalFolder = Environment.getExternalStorageDirectory();
File imageTemp = new File(externalFolder.getPath() + "/Reader/temp/cover");
if (!imageTemp.exists()) {
imageTemp.mkdirs();
}
for (int i = 0; i < books.length(); i++) {
JSONObject jsonObjectBook = books.getJSONObject(i);
String id = jsonObjectBook.getString("_id");
String title = jsonObjectBook.getString("title");
String author = jsonObjectBook.getString("author");
String shortIntro = jsonObjectBook.getString("shortIntro");
String cover = jsonObjectBook.getString("cover");
cover = URLDecoder.decode(cover);
String coverPath = imageTemp.getPath() + "/" +
id + ".jpg";
String site = jsonObjectBook.getString("site");
int banned = jsonObjectBook.getInt("banned");
int latelyFollower = jsonObjectBook.getInt("latelyFollower");
String retentionRatio = Integer.toString(jsonObjectBook.getInt
("retentionRatio"));
Book book = new Book(id, title, author, shortIntro, cover, site, banned,
latelyFollower, retentionRatio);
book.setCoverPath(coverPath);
mBookList.add(book);
}
Message message = new Message();
message.what = 0;
mHandler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
private void initRecyclerView() {
RecyclerView recyclerView = mView.findViewById(R.id.fragment_hot_recycler_view);
mBookRecyclerViewAdapter = new BookRecyclerViewAdapter(mBookList,
getActivity());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(mBookRecyclerViewAdapter);
}
}
根本没有运行,我无法弄清楚如何处理它。
<?php
return [
'driver' => env('FCM_PROTOCOL', 'http'),
'log_enabled' => true,
'http' => [
'server_key' => config('app.fcm_mode') == 'user' ? env('FCM_SERVER_KEY', 'Your FCM server key') : env('FCM_DELIVERY_SERVER_KEY', 'Your FCM server key'),
'sender_id' => config('app.fcm_mode') == 'user' ? env('FCM_SENDER_ID', 'Your sender id') : env('FCM_DELIVERY_SENDER_ID', 'Your sender id'),
'server_send_url' => 'https://fcm.googleapis.com/fcm/send',
'server_group_url' => 'https://android.googleapis.com/gcm/notification',
'timeout' => 30.0, // in second
],
];
答案 0 :(得分:1)
您需要致电sendMessage
这样的事情:
Message message = new Message();
message.what = 0;
message.obj = something;
mHandler.sendMessage(message);
您应该使用AsyncTask进行此类操作。
答案 1 :(得分:0)
用asynctask替换线程,如下所示
new AsyncTask<Void, Void, Void>(){
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
initRecyclerView();
}
@Override
protected Void doInBackground(Void... voids) {
URL url;
InputStream inputStream = null;
try {
url = new URL("http://api.zhuishushenqi.com/cats/lv2/statistics");
inputStream = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject jsonObject = new JSONObject(builder.toString());
JSONArray books = jsonObject.getJSONArray("books");
File externalFolder = Environment.getExternalStorageDirectory();
File imageTemp = new File(externalFolder.getPath() + "/Reader/temp/cover");
if (!imageTemp.exists()) {
imageTemp.mkdirs();
}
for (int i = 0; i < books.length(); i++) {
JSONObject jsonObjectBook = books.getJSONObject(i);
String id = jsonObjectBook.getString("_id");
String title = jsonObjectBook.getString("title");
String author = jsonObjectBook.getString("author");
String shortIntro = jsonObjectBook.getString("shortIntro");
String cover = jsonObjectBook.getString("cover");
cover = URLDecoder.decode(cover);
String coverPath = imageTemp.getPath() + "/" +
id + ".jpg";
String site = jsonObjectBook.getString("site");
int banned = jsonObjectBook.getInt("banned");
int latelyFollower = jsonObjectBook.getInt("latelyFollower");
String retentionRatio = Integer.toString(jsonObjectBook.getInt
("retentionRatio"));
Book book = new Book(id, title, author, shortIntro, cover, site, banned,
latelyFollower, retentionRatio);
book.setCoverPath(coverPath);
mBookList.add(book);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}.execute();