Reputation: 308
I recently started working on android, now i am working with AsyncTask how can i get response which is returned from the API below is the code.Every suggestion is appreciable.
class signmeup extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(WolfActivity.this);
pDialog.setMessage("Loading");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("section","user"));
params.add(new BasicNameValuePair("action","new"));
params.add(new BasicNameValuePair("device_type","2"));
params.add(new BasicNameValuePair("device_token","dhdkhgkdfhgkhfdghkdfjhgkjdfhgkdfhkhkHKhdkhsdkhKJHKWJHDSKAHDKJSAHJKDfhkashfkdjhfkjhskjhKJHJk"));
params.add(new BasicNameValuePair("first_name",fname));
params.add(new BasicNameValuePair("last_name",lname));
params.add(new BasicNameValuePair("email",email));
params.add(new BasicNameValuePair("phone",phone));
params.add(new BasicNameValuePair("fax",fax));
params.add(new BasicNameValuePair("address",addr));
params.add(new BasicNameValuePair("address1",addr1));
params.add(new BasicNameValuePair("facility",facility));
params.add(new BasicNameValuePair("password",pwd));
params.add(new BasicNameValuePair("zip",zipcode));
params.add(new BasicNameValuePair("city",city));
params.add(new BasicNameValuePair("state",state));
params.add(new BasicNameValuePair("how_you_found",huf));
params.add(new BasicNameValuePair("how_you_found_value",hufv));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest("http://eastendwebsolutions.com/wf/ws/", "GET", params);
Log.d("First Name",fname);
Log.d("Last Name",lname);
Log.d("Email",email);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
String success = json.getString("status");
String message = json.getString("message");
if(success == "0") {
Toast.makeText(WolfActivity.this,message ,Toast.LENGTH_LONG).show();
} else {
Toast.makeText(WolfActivity.this,"Error "+message ,Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("deprecation")
//@Override
protected void onPostExecute(String Result) {
if(pDialog.isShowing()){
pDialog.dismiss();
}
Log.d("Result",Result);
AlertDialog.Builder builder=new AlertDialog.Builder(WolfActivity.this);
builder.setTitle("Info");
builder.setMessage("Successfully registered");
builder.setIcon(R.drawable.app_icon);
builder.create().show();
}
}
Above code is terminates with error
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference at zonup.wolf.WolfActivity$signmeup.onPostExecute(WolfActivity.java:222) at zonup.wolf.WolfActivity$signmeup.onPostExecute(WolfActivity.java:135)
Error because this statement Log.d("Result",Result); in function onPostExecute(String Result)
I tried to alert the user with status in doInBackground() method but no toasts are coming:( is it correct??
Upvotes: 0
Views: 1020
Reputation: 1373
Just try this way you can see the message you get in the json response.
class signmeup extends AsyncTask<String, String, String> {
String message="";
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(WolfActivity.this);
pDialog.setMessage("Loading");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("section","user"));
params.add(new BasicNameValuePair("action","new"));
params.add(new BasicNameValuePair("device_type","2"));
params.add(new BasicNameValuePair("device_token","dhdkhgkdfhgkhfdghkdfjhgkjdfhgkdfhkhkHKhdkhsdkhKJHKWJHDSKAHDKJSAHJKDfhkashfkdjhfkjhskjhKJHJk"));
params.add(new BasicNameValuePair("first_name",fname));
params.add(new BasicNameValuePair("last_name",lname));
params.add(new BasicNameValuePair("email",email));
params.add(new BasicNameValuePair("phone",phone));
params.add(new BasicNameValuePair("fax",fax));
params.add(new BasicNameValuePair("address",addr));
params.add(new BasicNameValuePair("address1",addr1));
params.add(new BasicNameValuePair("facility",facility));
params.add(new BasicNameValuePair("password",pwd));
params.add(new BasicNameValuePair("zip",zipcode));
params.add(new BasicNameValuePair("city",city));
params.add(new BasicNameValuePair("state",state));
params.add(new BasicNameValuePair("how_you_found",huf));
params.add(new BasicNameValuePair("how_you_found_value",hufv));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest("http://eastendwebsolutions.com/wf/ws/", "GET", params);
Log.d("First Name",fname);
Log.d("Last Name",lname);
Log.d("Email",email);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
String success = json.getString("status");
message = json.getString("message");
if(success == "0") {
Toast.makeText(WolfActivity.this,message ,Toast.LENGTH_LONG).show();
} else {
Toast.makeText(WolfActivity.this,"Error "+message ,Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
message=e.toString();
e.printStackTrace();
}
return message;
}
@SuppressWarnings("deprecation")
//@Override
protected void onPostExecute(String Result) {
if(pDialog.isShowing()){
pDialog.dismiss();
}
Log.d("Result",Result);
AlertDialog.Builder builder=new AlertDialog.Builder(WolfActivity.this);
builder.setTitle("Info");
builder.setMessage(Result);
builder.setIcon(R.drawable.app_icon);
builder.create().show();
}
}
Upvotes: 2