我正在创建一个class
来检查给定的URL
是否有效,但我没有理解如何将此类调用到其他类中,因为在我的项目中,我必须检查每个班级的URL
回复。我的代码是:`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
MyTask task = new MyTask();
task.execute();
}
}
private class MyTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(10 * 1000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
Log.wtf("Connection", "Success !");
return true;
} else {
return false;
}
} catch (MalformedURLException e1) {
return false;
} catch (IOException e) {
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
boolean bResponse = result;
}
}
答案 0 :(得分:1)
你快到了!
private class MyTask extends AsyncTask<Void, Void, Boolean> {
private URL mUrl;
public MyTask(URL url) {
mUrl = url;
}
@Override
protected Boolean doInBackground(Void... params) {
try {
HttpURLConnection urlc = (HttpURLConnection) mUrl.openConnection();
urlc.setConnectTimeout(10 * 1000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
Log.wtf("Connection", "Success !");
return true;
} else {
return false;
}
} catch (MalformedURLException e1){
return false;
} catch (IOException e) {
return false;
}
}
}
当您需要检查责任时:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
URL url = new URL("http://www.google.com");
MyTask task = new MyTask(url){
@Override
protected void onPostExecute(Boolean result) {
if (result) {
// Do your work here
}
}
};
task.execute();
}
}
答案 1 :(得分:0)
首先,您可以为api调用创建一个常见的异步类:
public class BackgroundNetwork extends AsyncTask<Void, String, Void> {
private ProgressDialog progressDialog;
Context context;
public BackgroundNetwork(Context activity) {
context = activity;
progressDialog = new ProgressDialog(context);
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(progressDialog.isShowing()){
progressDialog.dismiss();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
progressDialog.setCancelable(false);
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
}
其次,您可以在活动中使用async类,并调用如下:
new BackgroundNetwork(context){
protected Void doInBackground(Void... params) {
checkUrl();
return null;
};
protected void onPostExecute(Void result) {
};
}.execute();
private checkUrl(){
URL myurl = new URL(url);
URLConnection connection = myurl.openConnection();
connection.setConnectTimeout(30 * 1000); // optional depends on you
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = -1;
try {
responseCode = httpConnection.getResponseCode();
} catch (Exception e1) {
throw e1;
}
if (responseCode == HttpURLConnection.HTTP_OK) {
//connection is OK and do what you want
//...
}
else
{
//connection is not OK
httpConnection.disconnect();
}
}