【问题标题】:cannot Load URL with android无法使用 android 加载 URL
【发布时间】:2014-09-17 20:49:25
【问题描述】:

我想检查应用程序的更新;我使用此功能,但它不起作用,并在第一次和第二次尝试中捕获。这是什么问题?

当你想下载页面时,你可以看到它的一些日志:

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)
                        {

                        }
                    }
                }
            });
        }

【问题讨论】:

    标签: android android-networking android-internet


    【解决方案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);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多