0

I have register activity and using Async taks to send the user-details to server. I want to get the response from server if the registeration was successfull or not.

Here is the code for my async task

class backgroundTask_register_activity extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... voids) {

        String url = "http://13.232.207.9/php/register.php";

        final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new BasicNameValuePair("user_id",user_id));
        parameters.add(new BasicNameValuePair("user_fullname",reg_user_fullname.getText().toString()));
        parameters.add(new BasicNameValuePair("user_name",reg_user_name.getText().toString()));
        parameters.add(new BasicNameValuePair("user_email",reg_user_email.getText().toString()));
        parameters.add(new BasicNameValuePair("user_mobile",reg_user_mobile.getText().toString()));
        parameters.add(new BasicNameValuePair("user_city",reg_user_city.getText().toString()));
        parameters.add(new BasicNameValuePair("user_password",reg_user_password.getText().toString()));
        parameters.add(new BasicNameValuePair("user_dob",DOB_textView.getText().toString()));
        parameters.add(new BasicNameValuePair("user_gender",reg_user_gender));

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {
            httppost.setEntity(new UrlEncodedFormEntity(parameters));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            if(entity != null){
                Toast toast = Toast.makeText(getApplicationContext(),EntityUtils.toString(entity),Toast.LENGTH_SHORT);
                toast.show();
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Response from the php is {"success":1,"Message":"User added successfully"}

So I want to display a toast notification for the same. What changes do I've to make to get the response?

But my code force closes the application:

he is the error log:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.prasaurus.app.psa_b2c_app, PID: 10201
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:309)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:200)
        at android.os.Handler.<init>(Handler.java:114)
        at android.widget.Toast$TN.<init>(Toast.java:356)
        at android.widget.Toast.<init>(Toast.java:101)
        at android.widget.Toast.makeText(Toast.java:266)
        at com.prasaurus.app.psa_b2c_app.RegisterActivity$backgroundTask_register_activity.doInBackground(RegisterActivity.java:145)
        at com.prasaurus.app.psa_b2c_app.RegisterActivity$backgroundTask_register_activity.doInBackground(RegisterActivity.java:119)
        at android.os.AsyncTask$2.call(AsyncTask.java:295)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
        at java.lang.Thread.run(Thread.java:818) 
Prashant Luhar
  • 299
  • 5
  • 19

3 Answers3

1

You need to add following things in your try block.

 try {
            httppost.setEntity(new UrlEncodedFormEntity(parameters));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            if(entity != null){
               Toast toast = Toast.makeText(getApplicationContext(),EntityUtils.toString(entity),Toast.LENGTH_SHORT);
               toast.show();
            }
      }

you can use Toast.LENGTH_LONG for long tome toast.

PushpikaWan
  • 2,437
  • 3
  • 14
  • 23
  • what is the error u got while executing this ?@PrashantLuhar – PushpikaWan Nov 02 '18 at 11:47
  • W/WindowAnimator: Failed to dispatch window animation state change. android.os.DeadObjectException at android.os.BinderProxy.transactNative(Native Method) – Prashant Luhar Nov 02 '18 at 11:50
  • This is not error what I want. This will pop when it not responsive.@PrashantLuhar can u put root error fromstack trace or. when u get erorr ? while getting "response.getEntity()" like. you can find it when you on it as a debugging process – PushpikaWan Nov 02 '18 at 11:59
  • java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:309) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) – Prashant Luhar Nov 02 '18 at 12:22
0

After HttpResponse response = httpclient.execute(httppost) and before your catch statements, parse the part of the Json response you need, write it to a string variable and create a Toast passing this variable as a parameter.

0

Add Toast with the response from server,

 try {
            httppost.setEntity(new UrlEncodedFormEntity(parameters));
            HttpResponse response = httpclient.execute(httppost);
        Toast.makeText(getApplicationContext(),"Response: "+response ,Toast.LENGTH_SHORT).show();


        }