Geting null from ArrayList in Android when extract JSONArray - java

I'm using Volley to get information from an SQL database, from server, and I get it in a JSONArray. When I extract values from the JSONArray so I can save values to an ArrayList, in the return statement I get null in my ArrayList. Can anyone help with the return statement?
#Override
public List<WorkItem> getWorkItem() {
final String url = "http://10.0.2.2:8080/items/all/items";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d("Response", response.toString());
try {
arrayList = new ArrayList<>();
response = new JSONArray(response.toString());
for (int i = 0; i < response.length(); i++) {
JSONObject jsonobject = response.getJSONObject(i);
String title = jsonobject.getString("title");
String description = jsonobject.getString("description");
String state = jsonobject.getString("status");
String assignee = jsonobject.getString("assignee");
arrayList.add(new WorkItem(title, description, state, assignee));
arrayList.size();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error", error.getMessage());
}
});
jsonArrayRequest.setTag(REQUEST_TAG);
mQueue.add(jsonArrayRequest);
return arrayList;
}

Related

display json array in textview android not working

My JSON array don't work and I don't know why. In logs, it only show this: [] and nothing else. I won't use that in text view.
This is my code:
private void account(){
// get from the server
String url = "my url";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("uaccount");
Log.i("conso", jsonArray.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject employee = jsonArray.getJSONObject(i);
Log.i("con", employee.toString());
//Log.i("con", name+ number+status+ age+ city+ town+ gender+weight +height);
String city = employee.getString("city");
String town = employee.getString("town");
txt_City.setText(""+city);
txt_Town.append(town);
//Log.i("co", city + town);
}
} catch (JSONException e) {
Log.i("console.logs e:", e.toString());
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i("console.logs error:", error.toString());
error.printStackTrace();
}
});
requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);
}
i tried this code with id :
in onreate:
uAccount("3");
in method
private void uAccount(final String id){
// get from server
String url = "my url";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("uaccount");
Log.i("conso", jsonArray.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject employee = jsonArray.getJSONObject(i);
Log.i("con", employee.toString());
//Log.i("con", name+ number+status+ age+ city+ town+ gender+weight +height);
String city = employee.getString("city");
String town = employee.getString("town");
txt_City.setText(""+city);
txt_Town.append(town);
//Log.i("co", city + town);
}
} catch (JSONException e) {
Log.i("console.logs e:", e.toString());
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i("console.logs error:", error.toString());
error.printStackTrace();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("id",id);
return params;
}
};
requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);
}

I want to create an ArrayList with a dynamic index not starting from 0

public void jsonParse(){
jsonResArray = new ArrayList< >();
String url = "my-api-url";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new
Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("data");
if(jsonArray.length()!=0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int id =Integer.parseInt(jsonObject.getString("service_id"));
**jsonResArray.add(id, jsonObject.getString("service_name"));**
}}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getBaseContext() , e.toString() , Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
});
mQueue.add(request);
}
I want to create dynamic index as i want to save service name against its id in string array but its giving me index out of bp
Thanks in advance
Use a Map<Integer, String> instead of an ArrayList<String>.

why response eror listener is but response listener is in the json array request?

my app it has to take data from server
and put it in the array list with recycler view
Listener dosent working and when i run the app and it just open Response.ErrorListener
i cant find what is wrong with tHAT
AND I HAVE TRIED IN SO MANY DEVICES
private void gitems()
{
String url="http://novindevelopers.ir/jsonarrayrequest.json";
final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("در حال دریافت اطلاعات");
progressDialog.setCancelable(false);
progressDialog.show();
Response.Listener<JSONArray> arrayListener = new Response.Listener<JSONArray>()
{
#Override
public void onResponse(JSONArray response)
{
try {
//JSONObject jsonObject = null;
for (int j = 0; j < response.length(); j++) {
JSONObject jsonObject = response.getJSONObject(j);
String name = jsonObject.getString("name");
String email = jsonObject.getString("email");
String phone = jsonObject.getString("phone");
items.add(new modellitems(name,email,phone));
}
}catch (JSONException e)
{
e.printStackTrace();
}
progressDialog.dismiss();
adaptorr.notifyDataSetChanged();
}
};
Response.ErrorListener errorListener = new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(MainActivity.this, "this", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
};
JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET ,url , null , arrayListener,errorListener );
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(arrayRequest);
}

i got error about java.lang.String cannot be converted to JSONarray

In my code, I am trying to populate value spinner from a MySQL database, but I can't get the value.
What's wrong on my jsonArray?
java.lang.String cannot be converted to JSONObject
JsonArrayRequest jsonSpinnerObjectRequest = new JsonArrayRequest(fetch_city_url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
JSONObject jsonObject = null;
JSONArray jsonArray = jsonObject.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject city = jsonArray.getJSONObject(i);
Log.i("onResponseSpinner", city.getString("kd_kelas") + " " + city.getString("kelas"));
kelasList.add(city.getString("kelas"));
}
spinnerAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY","ERROR in response"+error.getMessage());
}
});
Here you set a JSON object to null then try to get JSON Array from it:
JSONObject jsonObject = null;
JSONArray jsonArray = jsonObject.getJSONArray("result");
I think you are wanting something like this:
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray = jsonArray = new jsonObject.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject city = jsonArray.getJSONObject(i);
Log.i("onResponseSpinner", city.getString("kd_kelas") + " " + city.getString("kelas"));
kelasList.add(city.getString("kelas"));
}
spinnerAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
Looking at your code, i think you are already getting the JSON array in response so just get the values from this JSON array only, please go through the following code
JsonArrayRequest jsonSpinnerObjectRequest = new JsonArrayRequest(fetch_city_url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject city = response.getJSONObject(i);
Log.i("onResponseSpinner", city.getString("kd_kelas") + " " + city.getString("kelas"));
kelasList.add(city.getString("kelas"));
}
spinnerAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY","ERROR in response"+error.getMessage());
}
});

Return array from external function and get the result

I'm having a problem in Java.
I have this function "fillArray()" and I need this function return an array. The arrays values are get by JSON.
The code of this function is the next:
public String[] fillArray(){
requestQueue = Volley.newRequestQueue(getActivity());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://api.mysite.com/v1/countries",
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("data");
final String[] countries = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++){
JSONObject country = jsonArray.getJSONObject(i);
String country_name = country.getString("country_name");
countries[i] = country_name;
Log.v("NAME:", country_name);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
Log.e("VOLLEY", "ERROR");
}
}
);
requestQueue.add(jsonObjectRequest);
return countries;
}
I can return the array, I get the error "Cannot resolver symbol countries".
Any idea? Thanks in advance!

Categories