Create a Handler
in your Activity
or Fragment
, and pass the result of the HTTP call to it.
In your Activity
/Fragment
:
private final static int HANDLER_FROM_HTTP = 1;
private final Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
final int what = msg.what;
switch(what) {
case HANDLER_FROM_HTTP: {
Bundle b = msg.getDate();
if (b != null) {
String s = b.getString("result");
doSomething(s);
}
}
}
}
};
Then, in the run()
method of your Thread
above:
String str=EntityUtils.toString(rp.getEntity());
Message msg = pThreadHandler.obtainMessage(HANDLER_FROM_HTTP);
Bundle b = new Bundle();
b.putString("result", str);
msg.setData(b);
msg.sendToTarget();
This will allow you to execute doSomething()
on the main UI thread to update a View
, or perform some other action.