在url请求完成之前,Android小部件remoteView会更新

时间:2016-07-01 17:06:56

标签: android android-widget

我有widgetProvider和GetData类。

点击按钮后,我必须这样做 1.运行onUpdate()

  1. 禁用按钮并显示进度条

  2. 发送网址请求

  3. 请求完成后
  4. - 显示按钮并隐藏进度条

  5. 在appProvider onUpdate中我有代码

    rv.setInt(R.id.updateProgressBar, "setVisibility", View.VISIBLE);
    rv.setInt(R.id.BtnUpdate, "setVisibility", View.GONE);
    
    // Here I invoke method with http request from GetData class
    JSONArray data = getData.get();
    
    // Here is all correct, data comes well
    Log.d(LOG_TAG, "Get data: " + data.toString());
    
    // This runs before data is ready
    rv.setInt(R.id.updateProgressBar, "setVisibility", View.GONE);
    rv.setInt(R.id.BtnUpdate, "setVisibility", View.VISIBLE);
    

    GetData类,方法get()

    public JSONArray get() throws JSONException {
    
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    
            HttpClient httpclient = new DefaultHttpClient();
            String Url = "http://url";
            HttpGet httpget = new HttpGet(Url);
            JSONArray res = null;
    
            try{
                httpget.setHeader("Accept", "application/json");
                httpget.setHeader("Content-type", "application/json");
                HttpResponse response = httpclient.execute(httpget);
    
                if(response != null)
                {
                    InputStream is = response.getEntity().getContent();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    String json = reader.readLine();
                    JSONTokener tokener = new JSONTokener(json);
                    res = new JSONArray(tokener);
                }
    
            }catch (ClientProtocolException e) {
    
            } catch (IOException e) {
    
            }
            return res;
        }
    

    问题是显示隐藏后按钮后退隐藏。它不会等待回应。 我做错了什么?也许我需要使用某种承诺?

    向上

0 个答案:

没有答案