2013-02-06 49 views
0

早上好。 我想发送请求并在TextView中显示响应。使用异步Http客户端获得响应

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

我打电话从这个类

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

在活动我有一个处理程序获得的消息,并设置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 
+1

你确定你已经宣布所有的意见? –

+0

是的,我认为是。 – Valeriy

+0

错误告诉我们找不到某些请求的资源。仔细检查你的xml布局并清理/重建你的项目。 –

回答

1

,你可以在这里看到Message. what是int.you逝去的int的TextView内搭CharSequence作为参数setText方法。改变你的代码为:

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

OR

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