Issue
I cant handle onpostexecute result because doInBackground seems to return an object that i should convert to string in order to show.
this is the screen with the response of OnPostExecute:
the question is how obtain JSON RESPONSE
public class PostTask extends AsyncTask<URL, String, String> {
String error = "";
boolean flag = false;
Context mContext = null;
public PostTask(Context context) {
mContext = context;
}
@Override
protected String doInBackground(URL... data) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://example.it");
HttpResponse response = null;
try {
//add data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>( 1);
nameValuePairs.add(new BasicNameValuePair("username", "xxxxxxxxxx"));
nameValuePairs.add(new BasicNameValuePair("password", "xxxxxxxxxx"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//execute http post
response = httpclient.execute(httppost);
} catch (Exception e) {
Toast.makeText(mContext, e.getMessage(),Toast.LENGTH_LONG).show();
flag = true;
error = e.getMessage();
e.printStackTrace();
}
return response.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(flag){
Toast.makeText(mContext, "HttpHostConnectException Occured: "+error, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "DONE "+s, Toast.LENGTH_SHORT).show();
}
}
Solution
THIS WORKED FOR ME:
public class PostTask extends AsyncTask<URL, String, String> {
String error = "";
boolean flag = false;
Context mContext = null;
public PostTask(Context context) {
mContext = context;
}
@Override
protected String doInBackground(URL... data) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://example.com/");
HttpResponse response = null;
try {
//add data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>( 1);
nameValuePairs.add(new BasicNameValuePair("username", "xxxxxxx"));
nameValuePairs.add(new BasicNameValuePair("password", "xxxxx"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//execute http post
response = httpclient.execute(httppost);
} catch (Exception e) {
Toast.makeText(mContext, e.getMessage(),Toast.LENGTH_LONG).show();
flag = true;
error = e.getMessage();
e.printStackTrace();
}
try {
return convertHttpResponseToString(response);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (flag) {
Toast.makeText(mContext, "HttpHostConnectException Occured: " + error, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "DONE " + s.toString(), Toast.LENGTH_SHORT).show();
}
}
private String convertHttpResponseToString(HttpResponse response) throws IOException {
InputStream responseStream = response.getEntity().getContent();
Scanner scanner = new Scanner(responseStream, "UTF-8");
String responseString = scanner.useDelimiter("\\Z").next();
scanner.close();
return responseString;
}
}
Answered By - Giorgio Cafiso
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.