如何在Android Widget中使用loopj get

时间:2017-09-17 01:28:48

标签: android android-widget loopj

我正在尝试将Wson http请求与Widget一起使用,但在控制台上只有一条消息没有任何反应

  

D / dalvikvm:GC_CONCURRENT释放366K,5%免费10713K / 11207K,暂停5ms + 30ms   V / AsyncHttpRH:进展183从121(151%)

见下面的代码

public class MyWidget extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int count = appWidgetIds.length;

    for (int i = 0; i < count; i++) {
        int widgetId = appWidgetIds[i];

        String number = getCurrentPrice();
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.bitcoin_widget);
        remoteViews.setTextViewText(R.id.textView, number);

        Intent intent = new Intent(context, MyWidget.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.actionButton, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
}

String value = "0.00";
private String getCurrentPrice() {
    value = "0.00";
    String url = "https://www.mywebservice.net/stock/";
    AsyncHttpClient client = new AsyncHttpClient();
    client.get(url, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            try {
                String data= new String(responseBody, "UTF-8");
                JSONObject res = new JSONObject(data);
                JSONObject obj = res.getJSONObject("values");
                String last = obj.getString("stock");
                value = last;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            Log.d("STK", error.getMessage());
        }
    });
    return String.format("USD %.2f",Double.parseDouble(value));
}

1 个答案:

答案 0 :(得分:0)

  

return String.format(“USD%.2f”,Double.parseDouble(value));

因为client.get()是异步调用,所以它会立即执行并转到return语句。这就是为什么你看不到任何变化,因为value仍为0.将回报放在onSuccess()内。您还需要从onFailure()返回一个值。

修改

由于onSuccess无效,您需要传入一个将在解析后调用的回调。

interface ResponseCallback {
     void success(String value);
}

然后,

getCurrentPrice(new ResponseCallback() {
     @Override
     void success(String value) {
          remoteViews.setTextViewText(R.id.textView, value);
     }
}

getCurrentPrice内部(不返回值) -

private void getCurrentPrice(ResponseCallback callback) {
    ...
    client.get(url, new AsyncHttpResponseHandler() {
         @Override
         public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
              try {
                  ...
                  String last = obj.getString("stock");
                  callback.success(last);
              } catch(...)
          }