我要检查app的更新;我使用这个功能,但它不起作用,去第一次和第二次尝试。这是什么问题?
当你想下载页面时,你可以看到它的一些日志:
http://pasteboard.co/hn2hx7t.png
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)
{
}
}
}
});
}
答案 0 :(得分:1)
您正在主线程上发出网络请求。主线程专用于UI操作,因此android强制您使用AsynTasks或Threads。
创建一个新的AsyncTask并将下载逻辑添加到doInBackground
方法并使用onPostExecute
谷歌的例子: http://developer.android.com/training/basics/network-ops/connecting.html
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);
}
}