无法使用android加载URL
我想检查应用程序的更新;我使用了这个函数,但它不起作用,在第一次和第二次尝试中,我就去捕捉。这有什么问题 当您想要下载页面时,可以看到一些日志:无法使用android加载URL,android,android-networking,android-internet,Android,Android Networking,Android Internet,我想检查应用程序的更新;我使用了这个函数,但它不起作用,在第一次和第二次尝试中,我就去捕捉。这有什么问题 当您想要下载页面时,可以看到一些日志: 您正在主线程上进行网络请求。主线程专用于UI操作,因此android强制您使用AsynTask或线程 创建一个新的AsyncTask并将下载逻辑添加到doInBackground方法中,并在onPostExecute 谷歌的例子: 私有类下载WebPagetTask扩展异步任务{ @凌驾 受保护的字符串doInBackground(字符串…URL)
您正在主线程上进行网络请求。主线程专用于UI操作,因此android强制您使用AsynTask或线程 创建一个新的AsyncTask并将下载逻辑添加到
doInBackground
方法中,并在onPostExecute
谷歌的例子:
私有类下载WebPagetTask扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
//params来自execute()调用:params[0]是url。
试一试{
返回下载URL(URL[0]);
}捕获(IOE异常){
return“无法检索网页。URL可能无效。”;
}
}
//onPostExecute显示异步任务的结果。
@凌驾
受保护的void onPostExecute(字符串结果){
setText(结果);
}
}
void CheckForUpdate()
{
handler.post(new Runnable() {
@Override
public void run() {
try
{
URL versionURL = new URL("http://sth.com/daycounter/moharam_day_counter.php?p=widget_version_code");
URL widgetURL = new URL("http://sth.com/daycounter/moharam_day_counter.php?p=widget_url");
URLConnection urlConnection = versionURL.openConnection();
BufferedReader bufferReader = new BufferedReader(new InputStreamReader( urlConnection.getInputStream()));
String version = bufferReader.readLine();
Log.i("WP", ""+version);
if(Integer.parseInt(version) >= Integer.parseInt(versionCode))
{
bufferReader.close();
urlConnection = widgetURL.openConnection();
bufferReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String widgetURLString = bufferReader.readLine();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(widgetURLString));
ShowNotifyCation("", "***", intent, 1, R.drawable.icon_image, R.raw.alarm_sound);
}
bufferReader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
if(!GetIsShowBeheshtNote())
{
try
{
URL beheshtIsCommingURL = new URL("http://http://sth.com/daycounter/moharam_day_counter.php?p=behesht_is_coming");
URL beheshtURL = new URL("http://sth.com/daycounter/moharam_day_counter.php?p=behesht_url");
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(beheshtIsCommingURL.openStream()));
String isComing = bufferReader.readLine();
if(Boolean.parseBoolean(isComing))
{
bufferReader.close();
bufferReader = new BufferedReader(new InputStreamReader(beheshtURL.openStream()));
String beheshtURLString = bufferReader.readLine();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(beheshtURLString));
ShowNotifyCation("", ******", intent, 2, R.drawable.icon_image, R.raw.alarm_sound);
SetIsShowBeheshtNote();
}
bufferReader.close();
}
catch(Exception e)
{
}
}
}
});
}
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
textView.setText(result);
}
}