Reputation: 51
I want check for update of app; I use this function but it doesn't work and go to catch in first and second try. what is the problem of this ?
you can see some log of it when want to download the page :
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)
{
}
}
}
});
}
Upvotes: 0
Views: 76
Reputation: 1918
You are making network requests on main thread. Main thread is dedicated to UI operations, so android enforces you to use AsynTasks or Threads.
Create a new AsyncTask and add your download logic to doInBackground
method and use the result in onPostExecute
Example from google: 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);
}
}
Upvotes: 1