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 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 file_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.
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
//keyword is cv
String url = "http://ws2.samdel.net/mongodb_api.php?s=" + et_dataInput.getText().toString();
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<>();
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("files_name");
file_category = jo.getString("files_category");
file_keyword = jo.getString("files_keyword");
final HashMap<String, String> data = new HashMap<>();
data.put("files_name", ""+file_name);
data.put("files_category", ""+file_category);
data.put("files_keyword", ""+file_keyword);
list.add(data);
}
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<HashMap<String,String>> arrayAdapter = new ArrayAdapter<>(
MainActivity.this, android.R.layout.simple_list_item_1, list);
lv_files.setAdapter(arrayAdapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("My error", "Error Occurred" + error);
}
});
//Add the request to the RequestQueue.
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
});
}
}
I added a new line trying to populate the list but it seems that JSONArray doesn't produce any result. Did i miss something?
I got this error
org.json.JSONException: Value {"data":[{"_id":{"$oid":"637d0868f79eb6bab4957151"},"files_name":"CV_Intern_Adella","files_category":"intern student","files_keyword":"cv, intern, job, experience, name, matric, university"}],"status":"OK"} at files 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.
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
SimpleAdapter adapter = new SimpleAdapter(admin.this,list,R.layout.feedback_list,
new String[]{"YOUR_KEY_NAME"},
new int[]{R.id.YOUR_TEXTVIEW_ID_IN_LISTVIEW});
listView.setAdapter(adapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("My error", "" + error);
}
});
// Add the request to the RequestQueue.
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
});
}
}