Here is my problem …
public static String response = "";
‘
public static String sendDataToServer(String data)
{
new Thread()
{
public void run()
{
//sending data to server
response = httpClient.execute(httppost, new BasicResponseHandler()).trim();
}
} .start();
return response;
}
When i call this method i’am getting empty string as response.
String res = sendDataToServer("my_data"); //res is always empty string
//...XXXXXXX code after calling the method
And always XXXXX code is getting executed before finishing the execution of sendDataToServer() method. Why res is always empty ? How do i get the correct response string ? How can i stop the XXXX code being executed before execution of sendDataServer() method ?
Thanks.
You can do:
this way you will wait until the thread finishes and jsut then continue.
However, keep in mind that waiting for thread to finish is dangerous in Android, especially in UI thread (that is the main thread). However, I assume that this thread is not in UI thread, because otherwise
AsyncTaskwould have been better solution (and more native).