晚上好。我在获得回复方面遇到一些麻烦。 我有两个类: MyHttpClient 具有 get() 方法,以及用于响应的字符串。
public class MyHttpClient {
private static final String BASE_URL = "http://pgu.com";
private static String response;
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
public static String getResponse() {
return response;
}
public static void setResponse(String response) {
response = response;
}
}
在第二节课中,我使用 GET 方法。 Html 正在 LogCat 中打印,但 setResponse 不起作用。如何获取响应字符串作为 MyHttpClient 的字段?
public class MyHttpClientUsage {
public MyHttpClientUsage(){
}
public void getInfoAbout() throws HttpException{
RequestParams params = new RequestParams();
params.put("a", "Static");
params.put("content", "47");
MyHttpClient.get("", params, new AsyncHttpResponseHandler(){
@Override
public void onSuccess(String response) {
System.out.println(response);
//Write HTML in LogCat(work)
MyHttpClient.setResponse(response); //doesn't work
}
});
}
}
最佳答案
您需要在 MyHttpClient
中使用 this.response = response
,因为当前您只是重置发送到方法中的参数。
最好将方法参数设置为 final
以避免这种情况。
关于java - Java内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14686538/