Okhttp返回null

时间:2017-11-18 15:39:51

标签: android okhttp3

为什么这段代码在Run中没有返回任何内容并在Debug on activity中返回好的结果? TextView在Debug中返回结果。

@Data
public class HttpRequest {

    private static String url="url_to_my_server";
    private String answer;
    private OkHttpClient client;

    public HttpRequest() {
        client = new OkHttpClient();
    }

    private void getContent(String method) {
        final Request request = new Request.Builder()
            .url(url+method)
            .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                answer = "Connection is fail";
                System.out.println(answer);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                answer = response.body().string();
                System.out.println("response.body().string()"+answer);
            }
        });
    }

    public String getStringContent(String method) {
        getContent(method);
        return answer;
    }
}

@Data
public class ScrollingActivity extends AppCompatActivity {

    private String response;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        //code

        HttpRequest request = new HttpRequest();
        String test = request.getStringContent("test_method");
        textView = (TextView) findViewById(R.id.textView);
        textView.setText(test);
    }
}

1 个答案:

答案 0 :(得分:0)

使用同步请求:

    public String getStringContent() {
    Request request = new Request.Builder() .url("https://publicobject.com/helloworld.txt") .build(); 
    try (Response response = client.newCall(request).execute())
     { 
   if (!response.isSuccessful()) 
    throw new IOException("Unexpected code " + response); 
    Headers responseHeaders = response.headers(); 
    for (int i = 0; i < responseHeaders.size(); i++) { 
    System.out.println(responseHeaders.name(i) + ": " + 
     responseHeaders.value(i)); 
   }
    System.out.println(response.body().string()); 

    return response.body().string();

    }

<强>更新

    @Data
public class ScrollingActivity extends AppCompatActivity {

    private String response;
    private TextView textView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        //code

        callAsyncMethod();

    }

    public void updateMyUI(String text) {
        textView = (TextView) findViewById(R.id.textView);
        textView.setText(text);

    }

    public void callAsyncMethod(){
           Thread thread = new Thread() {
        @Override
        public void run() {
        HttpRequest request = new HttpRequest();
            String test = request.getStringContent("test_method");
            runOnUiThread(new Runnable() {
              public void run() {
                 updateMyUI(test)
              }
           });
        }
    };

        thread.start();
    }


}