【问题标题】:How to call a URL in background using Android?如何使用 Android 在后台调用 URL?
【发布时间】:2012-12-27 08:40:35
【问题描述】:

我希望通过调用服务器上 Web 服务的 URL 链接将用户数据插入数据库: 前任: 这是链接:

http://mydomain.com/AndroidWebService.asmx/nInsertInfo?id=12&lat=23.2222&log=12322

所以我想在隐藏模式下调用这个 url。

【问题讨论】:

    标签: android


    【解决方案1】:

    使用android android AsyncTask 发布数据。当应用程序打开时,它在android后台工作。 AsyncTask 允许正确和轻松地使用 UI 线程。此类允许在 UI 线程上执行后台操作并发布结果,而无需操作线程和/或处理程序。

    谢谢。

    【讨论】:

      【解决方案2】:

      感谢大家提供宝贵的解决方案。

      我通过创建 WebView 做到了

      WebView myWebView = (WebView) findViewById(R.id.view1);
      myWebView.loadUrl(urll);
      

      然后我可以根据需要管理我的 WebView:隐藏它、对齐为错误或结果返回。

      【讨论】:

        【解决方案3】:

        也许这个例子会对你有所帮助:

        public class URLConnectionTask <Result> extends AsyncTask<URL, Void, InputStream> {
        
        
                @Override
                protected InputStream doInBackground(URL... params) {
                    InputStream is;
                    HttpUriRequest request;
                    URI uri;
                    HttpResponse response;
        
                    if (params.length == 0)
                        return null;
        
                    is = null;CredentialsProvider credProvider = new BasicCredentialsProvider();
        
                if (userName != null && userName.length() > 0 && password != null && password.length() > 0)
                    credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                        new UsernamePasswordCredentials(userName, password));
        
                //
                DefaultHttpClient http = new DefaultHttpClient();
                http.setCredentialsProvider(credProvider);
                //
        
                uri = URI.create(params[0].toString());
        
                if (isPost)
                    request = new HttpPost(uri);
                else
                    request = new HttpGet(uri);
        
                try {           
                    response = http.execute(request);
                    is = response.getEntity().getContent();
        
                    //Log.d(TAG, "doInBackground() response: "+EntityUtils.toString(response.getEntity()));
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        
        
               if (processingHandler != null && is != null)
                   processingResult = processingHandler.processResponse(is);// processingHandler is an instance which implements ProcessingHandler interface (ex. VizMarket). processResponse() is implemented on this class.
        
               return is;
            }
        
            @Override
                protected void onPostExecute (InputStream result) {
                    input = result;
        
                    if (result == null)
                        return;
                            //instruction for inserting data on db .... 
        
                    try {
                        result.close();
                    } catch (IOException e) {
                    }
                }
            }
        

        【讨论】:

        • 这个解决方案是先进的,但是当我使用它时,它给了我运行时错误??你有什么想法吗?
        • 伙计,我举这个例子是为了了解 AsyncTask 的工作原理。我在我的 android 项目中使用这个类来获取 json 结果,并在 ListActivity 上显示它们。我认为向您提供运行时错误是正常的,但是如果您提供更多关于您想要做什么的详细信息,也许我会帮助您。
        【解决方案4】:

        您应该在 AsyncTask 中调用 URL。例如:Return data from AsyncTask class

        【讨论】:

          【解决方案5】:

          你必须使用线程从服务器获取数据并插入到 DB 或在 android 中你可以使用 AsyncTask。

          【讨论】:

            【解决方案6】:

            你可以搜索一下 :ClickableSpan 并将您的插入代码放入 OnClick 正文中~ 就像:

            String yourTextStr;
                SpannableString sp = new SpannableString(yourTextStr);
                int start;//the start of your url in the text; 
                int end ;//the end of your url in the text; 
                sp.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        //Your Insert Code
                    }
                }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-12-26
              • 2015-10-03
              • 1970-01-01
              • 2013-10-17
              • 1970-01-01
              • 1970-01-01
              • 2018-04-12
              • 1970-01-01
              相关资源
              最近更新 更多