-3

I need to save data on database MySql

This is the code:

private void uploadMultipart(final String imageData,final String titolo,final String sottotitolo,final String data) {
        String tag_string_req = "req_register";

        StringRequest strReq = new StringRequest(Request.Method.POST,
                AppConfig.UPLOAD_URL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Register Response: " + response.toString());
               // hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (!error) {


                        Toast.makeText(getActivity(), "Aggiunto ai preferiti!", Toast.LENGTH_LONG).show();

                    } else {

                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getActivity(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Registration Error: " + error.getMessage());
                Toast.makeText(getActivity(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("url", imageData);
                params.put("titolo", titolo);
                params.put("sottotitolo", sottotitolo);
                params.put("data", data);
                params.put("id", id);


                return params;
            }

        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

but with this code I get this exception:

W/System.err: org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject
2
  • Can you also post the Response which you are trying to parse? Commented Dec 18, 2017 at 11:23
  • JSONArray jsonArray = new JSONArray(response.body.string()) Commented Dec 18, 2017 at 11:38

3 Answers 3

0

Here your using jsonArray to jsonObject

JSONObject jObj = new JSONObject(response);

Instead, Use like this

JSONArray json = new JSONArray(response);
Sign up to request clarification or add additional context in comments.

4 Comments

boolean error = json.getBoolean("error"); error is underline in red
Share your response json
Register Response: []
The response is JsonArray so you have to parse the response string as JsonArray.Try the above code
0

Most probably your response String from the onResponse(String response) method is a JSONArray, not a JSONObject (enclosed in [] brackets instead of {}), hence when you try to parse and convert it to JSONObject you get an error.

Change the JSONObject jObj = new JSONObject(response); line to JSONArray jArr = new JSONArray(response); - this should fix the issue.

Comments

0

On your code where you have written JSONObject you are passing a JSON Array means your response is in array type. write JSONArray jsonArray = new JSONArray(response) now you can access this array

For more conformation please make a comment I'll help you for sure.

Thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.