从onPostExecute更新textview

时间:2017-01-03 22:36:20

标签: android google-app-engine android-asynctask textview

我正在尝试编写代码,该代码将从Google Cloud后端获取数据并作为端点返回,从教程Hello Endpoints开始。https://cloud.google.com/appengine/docs/java/endpoints/helloendpoints-android-studio

我试图将结果作为textview而不是Toast。我试过了

package com.example.username.textviews;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neil.myapplication.backend.myApi.MyApi;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private TextView t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t = (TextView) findViewById(R.id.response);
        new EndpointsAsyncTask().execute(new Pair<Context, String>(this, "Manfred"));
    }
}

class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
    private static MyApi myApiService = null;
    private Context context;

    @Override
    protected String doInBackground(Pair<Context, String>... params) {
        if(myApiService == null) {  // Only do this once
            MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)
                    // options for running against local devappserver
                    // - 10.0.2.2 is localhost's IP address in Android emulator
                    // - turn off compression when running against local devappserver
                    .setRootUrl("http://10.0.2.2:8080/_ah/api/")
                    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                        @Override
                        public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(true);
                        }
                    });
            // end options for devappserver

            myApiService = builder.build();
        }

        context = params[0].first;
        String name = params[0].second;

        try {
            return myApiService.sayHi(name).execute().getData();
        } catch (IOException e) {
            return e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String result) {
        //Toast.makeText(context, result, Toast.LENGTH_LONG).show();
        t.setText(result);
    }
}

我已经为@_ id / response添加了必要的行以及activity_main.xml和对strings.xml的响应。

它不会在onPostExecute()中识别t,也不会使用findViewById。

当我怀疑这时我需要从数据存储中检索,即。 field1到textfield1等。

1 个答案:

答案 0 :(得分:0)

在执行导致TextView或其他窗口小部件重新显示的事情时,您需要在UI线程上。我没有亲自使用onPostExecute(),但使用了其他类似的API。

以下是一种方法:

t.post (new Runnable()
{
  @Override public void run()
  {
    t.setText (result);
  }
});