JavaOgreniyor
JavaOgreniyor

Reputation: 31

Get POST Result in JSON

I'm sending the data with the POST. I get the answer with JSON. If I use plain text instead of JSON, it's working but It doesn't work with JSON.

I do not receive any error screen in the application

if (status) = true -> Register OK

IMPORT:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import javax.xml.transform.Result;

JSON:

[{"status":true,"result":"registerSuccessful","message":"Register succeeded"}]

JAVA Code:

private void registernewuser() {
        StringRequest request = new StringRequest(Request.Method.POST, registerUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                try {

                    JSONObject jsonResponse = new JSONObject(response);
                    String RegisterResultString = jsonResponse.getString("status");

                    if (RegisterResultString.equals("true")) {
                        Toast.makeText(getApplicationContext(), "Successfully Registered User", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(getApplicationContext(), "Sorry, Error Registering New User", Toast.LENGTH_LONG).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("registeruser", "true");
                params.put("username", username.getText().toString().trim());
                params.put("userpassword", userpassword.getText().toString().trim());
                params.put("useremail", useremail.getText().toString());
                return params;
            }
        };
        Volley.newRequestQueue(this).add(request);
    }

I'm not having trouble building applications. But I can't find my fault.

Upvotes: 0

Views: 147

Answers (3)

Victor Semenovich
Victor Semenovich

Reputation: 576

Your JSON code is a JSONArray with a single JSONObject because of square brackets. So, just set type of your jsonResponse as JSONArray and fetch a JSONObject using the JSONArray.getJSONObject(0) method.

JSONArray jsonResponse = new JSONArray(response);
String registerResultString = jsonResponse.getJSONObject(0).getString("status");

Upvotes: 2

Maraj Hussain
Maraj Hussain

Reputation: 1596

You can call the request as JSONRequest

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                your_url, new com.android.volley.Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                    }
                }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }
        };

Upvotes: -1

Chuk Ultima
Chuk Ultima

Reputation: 1037

You need to add the header to your request to indicate the JSON usage :

request.addHeader("content-type", "application/json");

Else it will interpret it as plain text.

Upvotes: 0

Related Questions