无法解决response.body()。string()

问题描述 投票:0回答:2

我正在尝试将stringCentroid发送给其他活动类,但日志显示它为空字符串。


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?请帮我。谢谢:)

android httpresponse
2个回答
0
投票

您在启动请求后立即记录变量。将您的记录代码和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);

}

0
投票

将下面的代码放入runOnUIThread-> run()函数中,因为它在线程上运行,并且可以在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);
© www.soinside.com 2019 - 2024. All rights reserved.