我是 android 编程的新手,为了测试我有这个下载谷歌主页的代码。 代码有效,但我有一个问题:
如果我启动应用程序并且互联网正常,代码下载页面没有问题但是如果,例如,在下载期间互联网掉线我无法知道,因为没有方法会调用。
我该如何解决这个问题?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// called before request is started
Log.d ("info", "PRIMA DELLA CONNESSIONE");
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
// called when response HTTP status is "200 OK"
Log.d ("info", "DATI");
Log.d ("info", response.toString());
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
Log.d ("info", "errore");
Log.d ("info", Integer.toString(statusCode));
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
Log.d ("info", "RETRY");
}
});
}
最佳答案
这就是它的工作原理,但我认为有一种方法叫做,
onFinish,你可以设置请求时间,对于一些网络连接错误你可以把你的代码放在try-catch中
try
{
//your code here
}
catch(Exceptio e) // you can try catch SocketException
{}
如果你的连接有问题,你可以用 try 捕捉
关于java - AsyncHttpClient android 互联网问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38937556/