Getting JSON response from server after sending data in android studio

send json data to server android-volley
how to send data to server using json in android example
how to post json data to server in android using httpurlconnection
android volley get request example
json post method in android example
how to get json data from url in android using volley
android http request json
how to send json data to server in android using retrofit

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) 

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.

Make a standard request, Request JSON. This lesson Specify a URL and receive a raw string in response. Specify a URL and get a JSON object or array (respectively) in response. Volley provides the following classes for JSON requests: Developer guides · Design guides · API reference · Samples · Android Studio. JSON Parsing Tutorial With Example In Android Studio [Step by Step] JSON stands for JavaScript Object Notation. It is structured, light weight, human readable and easy to parse. It’s a best alternative to XML when our android app needs to interchange data from server. XML parsing is very complex as compare to JSON parsing.

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.

Android, Sending data to the cloud is a common task in mobile applications. We will use HttpURLConnection to make an HTTP POST request to the server. get-location Android Studio 3.5.1; HttpURLConnection client. When the user click send, the app will build a JSON object holding the three inputs, add  After this much, retrofit have make the http call to the URL and it has get the response. Compiler will read the JSON response in the onResponse() method. Below line will give us the JSON response in the String format. String jsonresponse = response.body().toString(); Then compiler will call the writeTv(jsonresponse) method. Compiler will pass the string variable which holds the JSON response in the parameter of this method.

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();


        }

Sending JSON Data to Server using Async Thread – Applied , Also I had to removed the below code after the “return JsonResponse;” line as the Android studio 2.2.3 Gradle build was throwing an error that it  How To Send A JsonObjectRequest From Android (Send GET Request For JSON Data) Another type of GET request you’re likely to need on Android is the JsonObjectRequest. A JsonObjectRequest enables you to send an HTTP request for JSON objects.

Fetch data dynamically, json that contains a JSON-formatted list of words. When you click the button, the app makes a GET request of the server and loads the file. Implementation note:  In This example calling restful webservice to get json data and parse that json data. Steps : 1. Call php file (php) from server and create JSON data in php.

Making a JSON POST Request With HttpURLConnection, In this tutorial, we'll demonstrate how to make a JSON POST request using with the target URI string that accepts the JSON data via HTTP POST method: ? invoke the openConnection method to get the HttpURLConnection object. This parameter has to be set to send the request body in JSON format. You’ll need to make a HTTP URL connection and read the response using, say, BufferedReader. See a sample code below: [code] URL url; StringBuffer response = new

Volley Get and Post JSONObject Request - TechPin, You can get and post any type of data using volley framework. client to get response from server and saves response in cache before sending parsed Android Networking With Volley - Get &Post JSONObject from web API  Communication between android application and server is one of the most common requirements. In frequent cases, you need to collect information from user in your android application and store it in the database for future use.

Comments
  • what is the error u got while executing this ?@PrashantLuhar
  • W/WindowAnimator: Failed to dispatch window animation state change. android.os.DeadObjectException at android.os.BinderProxy.transactNative(Native Method)
  • 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
  • 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)
  • Nope it just force closes the application
  • i think RegisterActivity.java:145 RegisterActivity.java:119 are doing the thing
  • stackoverflow.com/q/3875184/10268269 use this question as reference hope it may help