Home > Net >  com.android.volley.ParseError: org.json.JSONException error in android
com.android.volley.ParseError: org.json.JSONException error in android

Time:12-03

I'm trying out a simple coding to build an app with api using volley but get the error jsonexception of type org.json.JSONObject cannot be converted to JSONArray.

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

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

public class MainActivity extends AppCompatActivity {
    Button btn_Submit;
    EditText et_dataInput;
    ListView lv_files;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_Submit = findViewById(R.id.btn_Submit);
        et_dataInput = findViewById(R.id.et_dataInput);
        lv_files = findViewById(R.id.lv_files);

        btn_Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Instantiate the RequestQueue.
                RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
                String url = "http://ws2.samdel.net/mongodb_api.php";

                JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
                        new Response.Listener<JSONArray>() {
                            @Override
                            public void onResponse(JSONArray response) {
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    JSONArray result = jsonObject.getJSONArray("files");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                                Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void one rrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                        Log.d("Error.Response", error.toString());
                    }
                });
//              Add the request to the RequestQueue.
                queue.add(request);
            }
        });
    }
}

I got this error

com.android.volley.ParseError: org.json.JSONException: Value {"files":{"data":[{"_id":{"$oid":"637d0799f79eb6bab4957150"},"files_name":"G4_Proposal","files_category":"proposal","files_keyword":"workshop, proposal, student, project"},{"_id":{"$oid":"637d0868f79eb6bab4957151"},"files_name":"CV_Intern_Adella","files_category":"intern student","files_keyword":"cv, intern, job, experience, name, matric, university"}],"status":"OK"}} of type org.json.JSONObject cannot be converted to JSONArray

Does anyone know what seems to be the problem and how can i fix it? thank you in advance.

CodePudding user response:

package com.example.myapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
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.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    Button btn_Submit;
    EditText et_dataInput;
    ListView lv_files;

    String file_name;
    String file_category;
    String files_keyword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_Submit = findViewById(R.id.btn_Submit);
        et_dataInput = findViewById(R.id.et_dataInput);
        lv_files = findViewById(R.id.lv_files);

        btn_Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Instantiate the RequestQueue.
                String url = "http://ws2.samdel.net/mongodb_api.php";

            StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray result = jsonObject.getJSONArray("files");

                        for (int i = 0; i < result.length(); i  ) {
                            JSONObject jo = result.getJSONObject(i);
                            file_name = jo.getString("<YOUR KEY NAME for file_name>");
                            file_category = jo.getString("<YOUR KEY NAME for file_category>");
                            files_keyword = jo.getString("<YOUR KEY NAME for file_keyword>");

                            final HashMap<String, String> data = new HashMap<String, String>();
                            data.put("<YOUR KEY NAME for file_name>", "" file_name);
                            data.put("<YOUR KEY NAME for file_category>", "" file_category);
                            data.put("<YOUR KEY NAME for file_keyword>", "" files_keyword);

                            list.add(data);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    //you can add your array to list view in here
                    
                }
            }, new Response.ErrorListener() {
                @Override
                public void one rrorResponse(VolleyError error) {
                    Log.i("My error", ""   error);
                }
            });

//              Add the request to the RequestQueue.
            RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
            requestQueue.add(stringRequest);
        }
    });
}
}
  • Related