Android에서 애플리케이션을 개발 중입니다. 응용 프로그램은 특정 웹 서버에 HTTP 요청을 게시 할 수 있습니다. 게시물 요청은 비동기 적으로 실행되어야하므로 작업을 수행 할 스레드를 만듭니다. 하지만 스레드 끝에서 호출 될 콜백이 필요하며`post` 메서드를 호출하는 스레드에서 호출해야합니다.Android : 특정 스레드에서 메소드 실행
내 post
방법은 다음과 같습니다
interface EndCallback
{
public void Success(String response);
public void Fail(Exception e);
}
public void post(final String url, final List<NameValuePair> data, EndCallback callback)
{
Thread t = Thread.currentThread();
(new Thread()
{
public void run()
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try
{
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse r = httpclient.execute(httppost);
HttpEntity en = r.getEntity();
String response = EntityUtils.toString(en);
//I want to call callback.Success(response)
//here from thread t
}
catch (Exception ex)
{
//And I want to call callback.Fail(ex)
//here from thread t
}
}
}).start();
}
Handler, AsyncTask 또는 Activity 또는 View에 Runnable 인스턴스를 게시하는 등의 다양한 옵션에 대해서는 [Painless Threading] (http://developer.android.com/resources/articles/painless-threading.html)을 참조하십시오. –