我正在尝试获取AsyncTask类以连接到网站并检索HTTP状态代码。但是在doInBackground()中间,它只是停止运行,没有错误输出。根据检索到的状态码,然后在“ x” ImageView上设置“ x”图像。
执行AsyncTask:
new LoadImage(viewHolder.icon_imageView, model).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, model.getSite());
AsyncTask:
private static class LoadImage extends AsyncTask<String, Void, Integer> {
private final WeakReference<ImageView> imageViewWeakReference;
private final WeakReference<Model> modelWeakReference;
private ImageView imageView = null;
private Model model = null;
private String TAG = "auto";
public LoadImage(ImageView imageView, Model model){
Log.i(TAG, "LoadImage: ");
imageViewWeakReference = new WeakReference<ImageView>(imageView);
modelWeakReference = new WeakReference<Model>(model);
Log.i(TAG, "LoadImage: ");
}
@Override
protected Integer doInBackground(String... strings) {
Log.i(TAG, "doInBackground: ");
if(imageViewWeakReference != null && modelWeakReference != null){
imageView = imageViewWeakReference.get();
model = modelWeakReference.get();
try {
String site = model.getSite();
URL url = new URL(site);
if(site.startsWith("https")){
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.connect();
model.setStatuscode(httpsURLConnection.getResponseCode());
}
else{
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
model.setStatuscode(httpURLConnection.getResponseCode());
}
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.i(TAG, "doInBackground: " + model.getStatuscode());
return model.getStatuscode();
}
Log.i(TAG, "doInBackground: null");
return null;
}
@Override
protected void onPostExecute(Integer statuscode) {
Log.i(TAG, "onPostExecute: ");
if (isCancelled()) {
imageView = null;
}
if (imageView != null && statuscode == HttpURLConnection.HTTP_OK) {
imageView.setImageResource(R.mipmap.ic_checkmark_round);
}
else
{
imageView.setImageResource(R.mipmap.ic_cross_round);
}
Log.i(TAG, "onPostExecute: ");
}
}
控制台输出:
09-26 11:05:33.628 19788-19788 / com.frederikmillingpytlick.ut此处I / auto:LoadImage:
09-26 11:05:33.628 19788-19788 / com.frederikmillingpytlick.ut此处I / auto:LoadImage:
09-26 11:05:33.628 19788-19822 / com.frederikmillingpytlick.ut此处I / auto:doInBackground:
为什么不是控制台输出:
09-26 11:05:33.628 19788-19788 / com.frederikmillingpytlick.ut此处I / auto:LoadImage:
09-26 11:05:33.628 19788-19788 / com.frederikmillingpytlick.ut此处I / auto:LoadImage:
09-26 11:05:33.628 19788-19822 / com.frederikmillingpytlick.ut此处I / auto:doInBackground:
09-26 11:05:33.628 19788-19822 / com.frederikmillingpytlick.ut此处I / auto:doInBackground:model.getStatuscode()
09-26 11:05:33.628 19788-19822 / com.frederikmillingpytlick.ut此处I / auto:onPostExecute:
09-26 11:05:33.628 19788-19822 / com.frederikmillingpytlick.ut此处I / auto:onPostExecute:
最佳答案
在打开控制台输出一段时间后,出现超时错误。
好像我使用的URL没有响应。
适用于大多数其他URL。