Are you using multiple threads? Again, using the thread pool approach, the code doesn't seem to have a problem.
Seems to be a thread problem but I don't know how to solve it by taking out the url of all the images from the first url and adding them to the list again But occasionally this program will work and occasionally it won't, and it will have the following error.
private Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
byte[] result= (byte[]) msg.obj;
Bitmap bitmap= BitmapFactory.decodeByteArray(result,0,result.length);
list.add(bitmap);
recyclerView=findViewById(R.id.recyclerview);
linearLayoutManager=new LinearLayoutManager(MainActivity4.this);
adaptern1=new adaptern1(MainActivity4.this,list);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adaptern1);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
downAsynFile();
}
private void downAsynFile() {new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://mrobot.pcauto.com.cn/v2/cms/channels/3?pageNo=1&pageSize=20&v=4.0.0")
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
parseJSONWithGSON(responseData);
} catch (Exception e) {
e.printStackTrace(); }}
}).start(); }
private void parseJSONWithGSON(String jsonData) {
Gson gson = new Gson();
Data data=gson.fromJson(jsonData,Data.class);
showResponse(data); }
public void showResponse(Data data){runOnUiThread(new Runnable() {
@Override
public void run() {
List<Data.DataBean> dataBeans=data.getData();
for (Data.DataBean dataBean : dataBeans) {
String a=dataBean.getImage();
sendrequest(a);
} }});}
public void sendrequest(String a) {new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient=new OkHttpClient();
Request request=new Request.Builder()
.url(a)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
Message message=handler.obtainMessage();
message.obj=response.body().bytes();
handler.sendMessage(message);
}
}); }}).start();}}
The following is an error message
2023-02-20 18:31:31.612 5635-5665/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-3
Process: com.example.myapplication, PID: 5635
java.lang.IllegalArgumentException: Expected URL scheme 'http' or 'https' but no colon was found
at okhttp3.HttpUrl$Builder.parse$okhttp(HttpUrl.kt:1260)
at okhttp3.HttpUrl$Companion.get(HttpUrl.kt:1633)
at okhttp3.Request$Builder.url(Request.kt:184)
at com.example.myapplication.MainActivity4$4.run(MainActivity4.java:89)
at java.lang.Thread.run(Thread.java:923)
2023-02-20 18:31:31.612 5635-5666/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.example.myapplication, PID: 5635
java.lang.IllegalArgumentException: Expected URL scheme 'http' or 'https' but no colon was found
at okhttp3.HttpUrl$Builder.parse$okhttp(HttpUrl.kt:1260)
at okhttp3.HttpUrl$Companion.get(HttpUrl.kt:1633)
at okhttp3.Request$Builder.url(Request.kt:184)
at com.example.myapplication.MainActivity4$4.run(MainActivity4.java:89)
at java.lang.Thread.run(Thread.java:923)
0 Answer
这家伙很懒,什么都没留下...