gpt4 book ai didi

java - 使用异步 Http 客户端获取响应

转载 作者:行者123 更新时间:2023-12-01 14:52:11 26 4
gpt4 key购买 nike

早上好。我想发送获取请求并在 TextView 中显示响应。

public class MyHttpClient {

private static final String BASE_URL = "http://pgu.com";

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;
}}

我正在从此类调用 get

public class MyHttpClientUsage {

Handler h;

public MyHttpClientUsage(Handler h){
this.h = h;
}

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);
//Simplify sending int to TextView
MyHttpClientUsage.this.h.sendEmptyMessage(678);
}
});
}}

在 Activity 中,我有一个处理程序来获取消息并设置 TextView

public class MainActivity extends Activity {

Handler h;
TextView largeText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

largeText = (TextView) findViewById(R.id.textView1);

h = new Handler(){

public void handleMessage(android.os.Message msg){
largeText.setText(msg.what);
}

};

MyHttpClientUsage connect = new MyHttpClientUsage(h);
try {
connect.getInfoAbout();
} catch (HttpException e) {
e.printStackTrace();
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}

在 LogCat 中我收到此警告

02-06 14:30:51.783: W/dalvikvm(546): threadid=1: thread exiting with uncaught exception (group=0x409961f8)

和错误

02-06 14:30:51.833: E/AndroidRuntime(546): android.content.res.Resources$NotFoundException:   String resource ID #0x2a6

最佳答案

正如您在这里看到的 Message. what是一个 int。您将 int 传递给 TextViewsetText 方法,该方法以 CharSequence 作为参数。将您的代码更改为:

largeText.setText(String.valueOf(msg.what));

largeText.setText(msg.what.toString());

关于java - 使用异步 Http 客户端获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14732217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com