无法用 response.body().string() 解决问题

Can't solve problem with response.body().string()

我正在尝试将 stringCentroid 发送到其他 activity class 但日志显示它是空字符串。


public class Centroid extends AppCompatActivity{
    private static final String TAG = "result";
    TextView centroid;
    String stringUrl =".net/MeetingLocationCentroid?location=";
    String data = "{";
    private GeoApiContext mGeoApiContext = null;
    private GoogleMap map;
    String stringCentroid = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.centroid_result);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);


        centroid = findViewById(R.id.centroid);
        Intent intent = getIntent();
        ArrayList<AddressItem> addresses = (ArrayList<AddressItem>) intent.getSerializableExtra("locations");
        for (int i = 0; i < addresses.size(); i++) {
            data += addresses.get(i).latlng;
            if (i!=addresses.size()-1)
                data += ",";
        }
        stringUrl = stringUrl  + data+"}";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(stringUrl)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (response.isSuccessful()){
                    final String myResponse = response.body().string();
                    Centroid.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d(TAG, "myResponse: "+myResponse);

                            stringCentroid += myResponse;
                            centroid.setText(stringCentroid);

                        }
                    });
                }
            }
        });
        Log.d(TAG, "centroid textview = "+centroid.getText().toString());

        Log.d(TAG, "stringCentroid: "+stringCentroid);
        //Intent pushIntent = new Intent(Centroid.this, MainActivity.class);
        //pushIntent.putExtra("centroid",stringCentroid);
        //startActivity(pushIntent);
    }

}

这是我的代码。下面是我的日志

/result: stringCentroid:
/result: centroid textview = 
/result: myResponse: [37.440605, 126.892082]

日志显示 myResponse 是 String 但其他不是。怎么了?我怎样才能将字符串提供给 stringCentroid?请帮我。谢谢:)

您在启动请求后立即记录变量。将您的日志代码和 startActivity 放入 运行 函数中,如下所示:

public void run() {
    Log.d(TAG, "myResponse: "+myResponse);
    stringCentroid += myResponse;
    centroid.setText(stringCentroid);

    Log.d(TAG, "centroid textview = "+centroid.getText().toString());

    Log.d(TAG, "stringCentroid: "+stringCentroid);
    Intent pushIntent = new Intent(Centroid.this, MainActivity.class);
    pushIntent.putExtra("centroid",stringCentroid);
    startActivity(pushIntent);

}

将下面的代码放在 运行OnUIThread -> 运行() 函数中,因为它 运行 在线程上并且可以在 startActivity 函数之后执行。

Log.d(TAG, "centroid textview = "+centroid.getText().toString());
Log.d(TAG, "stringCentroid: "+stringCentroid);
Intent pushIntent = new Intent(Centroid.this, MainActivity.class);
pushIntent.putExtra("centroid",stringCentroid);
startActivity(pushIntent);