I am doing HTTP connection in AsyncTask. I am returning the string result of the query back to the calling code. There I am setting the value of EditText "HTTPResult" to the returned resultant string. But the HTTPResult is empty. This means nothing is returned to the calling code. However, I am getting the result string within the AsyncTask and can set HTTPResult inside protected void onPostExecute(String bitmap) method of AsyncTask. Then I used interface and delegates but did not get the result.
CODE:
public class RegisterActivity extends AppCompatActivity implements AsyncResponse {
Button btn_Connect;
EditText busNumber;
EditText text;
EditText HTTPResult;
String myServer="http://swulj.atwebpages.com/hi.php";
String myResult, token;
HTTPConnection1 conn = new HTTPConnection1();
RegisterActivity obj = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
btn_Connect = (Button) findViewById(R.id.button_Connect);
busNumber = (EditText) findViewById(R.id.Bus_number);
text = (EditText) findViewById(R.id.text);
HTTPResult = (EditText) findViewById(R.id.HTTPResult);
btn_Connect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
Toast.makeText(getApplicationContext(),"You download is resumed2",Toast.LENGTH_LONG).show();
conn.delegate = obj;
conn.execute(myServer, (String) busNumber.getText().toString());
//sleep(50000);
HTTPResult.setText(myResult);
/*StringTokenizer st = new StringTokenizer(myResult);
token = st.nextToken("<br>");
text.setText(token);
while (st.hasMoreTokens()) {
token = st.nextToken("<br>");
busNumber.setText(token);
}*/
}
});
}
@Override
public void processFinish(String output){
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
myResult = output;
}
class HTTPConnection1 extends AsyncTask<String, Void, String> {
String result;
String url;
String busNumber;
public AsyncResponse delegate = null;
@Override
protected String doInBackground(String... params) {
url = params[0];
busNumber = params[1];
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", busNumber));
nameValuePairs.add(new BasicNameValuePair("message", "msg"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpclient.execute(httppost);
InputStream inputStream = httpResponse.getEntity().getContent();
//HTTPResult.setText("result3");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
//HTTPResult.setText("result4");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//HTTPResult.setText("result5");
StringBuilder stringBuilder = new StringBuilder();
//HTTPResult.setText("result6");
String bufferedStrChunk = null;
while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
stringBuilder.append(bufferedStrChunk);
}
result = stringBuilder.toString();
//result= "Sandeep";
} catch (ClientProtocolException e) {
result = "ClientProtocolException";
// TODO Auto-generated catch block
} catch (IOException e) {
result = "IOException";
// TODO Auto-generated catch block
}
return result;
}
@Override
protected void onPostExecute(String bitmap) {
super.onPostExecute(bitmap);
//HTTPResult.setText(result);
delegate.processFinish(bitmap);
}
}
}
public interface AsyncResponse {
void processFinish(String output);
}
my motive is not to set text into HTTPResult, but to set the string myResult; I am testing values with HTTPResult