i new to android and i have developed an application to post and get the response from a php server.
AsyncTask
class MyTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... arg0) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://172.16.110.3/agent_tracking/index.php/api/rest/auth");
try {
// Add your data
List<BasicNameValuePair> nameValuePairs = new ArrayList<>(1);
nameValuePairs.add(new BasicNameValuePair("key", arg0[0]));
// nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
return responseString;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}
}
Main Class
Button login = (Button) findViewById(R.id.btn_login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.et_user_name);
EditText password = (EditText) findViewById(R.id.et_password);
String user_name = username.getText().toString();
String paswrd = password.getText().toString();
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
String data2 = user_name + "|" + paswrd + "|" + timeStamp;
byte[] data = new byte[0];
try {
data = data2.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
show_message(base64);
new MyTask().execute(base64);
//postData();
//startActivity(new Intent(MainActivity.this, mainmenu.class));
}
});
My problem is am returning a value called responseString
from the AsyncTask. I want to catch that value from the main class and display it in the message box.
i tried like this
String result = new MyTask().execute(base64);
i am getting the error incomparable type error.
Can some one help me to get the returned vale to the main class