here_is_json_api
And my code
ArrayRequest = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i = 0 ; i<response.length();i++) {
try {
// JSONObject jsonObject = (JSONObject) response.get(i);
jsonObject = response.getJSONObject(i);
JSONObject jsonObjectt = jsonObject.getJSONObject("search_result");
Users users = new Users();
users.setId(jsonObjectt.getString("id"));
users.setUser(jsonObjectt.getString("User"));
users.setName(jsonObjectt.getString("name"));
users.setWho(jsonObjectt.getString("who"));
users.setImage(jsonObjectt.getString("image"));
userlist.add(users);
}
catch (JSONException e) {
e.printStackTrace();
}
}
setRvadapter(userlist);
i can not fetch any data from api , Its says JSONObject cannot be converted to JSONArray in android
you are using JsonArray as a response and your web service return a jsonobject
new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray teachers = response.getJSONArray("search_result");
for(int i = 0; i < teachers.length() ; i++) {
// do what ever you want with
Teacher teacher = new Teacher();
// here is how to get name
teacher.setId(teachers.getJSONObject(i).getString("name"));
// do the rest like this.
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
Related
I want to fetch data from this file: https://api.covid19india.org/state_district_wise.json
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading data...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
"https://api.covid19india.org/state_district_wise.json",
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("Jammu and Kashmir");
progressDialog.cancel();
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
// I want get case details of "Jammu and Kashmir"
}
} catch (Exception e) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
What should I use here:
JSONArray jsonArray = jsonObject.getJSONArray("Jammu and Kashmir");
Data comming from API is JSONArray, not JSONObject
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, "https://api.covid19india.org/v2/state_district_wise.json",null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject dataOBJ = response.getJSONObject(i);
JSONArray jsonChild = dataOBJ.getJSONArray("districtData");
for (int k = 0; k < jsonChild.length(); k++) {
JSONObject obj = jsonChild.getJSONObject(k);
// work to be done...
}
}
} catch (Exception exp) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
The code is working fine now. Read this for complete sollution.
I want to access email and phone in HashMap from outside of the JSONArray Response. How can I do this?
I tried making globale variables and assign values to them. It didn't worked.
JsonArrayRequest request1 = new JsonArrayRequest(Request.Method.GET,
url,
null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
List<HashMap<String, String>> list = new ArrayList<>();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
HashMap<String, String> map = new HashMap();
map.put("email", obj.getString("email"));
map.put("phone", obj.getString("phone"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(HomeActivity.this, "Error"+error, Toast.LENGTH_SHORT).show();
}
});
Declare HashMap<String, String> map = new HashMap(); at top level in your class
And use it in your JsonArrayRequest
#Override
public void onResponse(JSONArray response) {
List<HashMap<String, String>> list = new ArrayList<>();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
map.put("email", obj.getString("email"));
map.put("phone", obj.getString("phone"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am using Volley to request unsplash API but when I try requesting it as JsonObjectRequest it doesn't give me any errors but I know that is a wrong approach because the data I am receiving is JSONArray
MY APPROACH
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
URL,
null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
if(response!=null){
// Process the JSON
try{
// Loop through the array elements
for (int i = 0; i < response.length(); i++) {
JSONObject js = response.getJSONObject(i);
Log.d("TESTING",js.getString("id"));
}
p.dismiss();
}catch (JSONException e){
e.printStackTrace();
}
}}
},
new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
Log.d("TESTING",error.getMessage());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
LOG
Since the JSONArray is too big to post here this is the formate
D/TESTING: org.json.JSONException: Value {"total": 44760,
"total_pages": 4476,
"results":[{JSONObjects}]}
If you wish to view the JSONArray here is the link "https://api.unsplash.com/search/photos?query=wood&client_id=ACESS_KEY_REQUIRED"
also I have viewed this question but that is totally different
You can request a key simply by making an account here.
You need to create jsonObject first. You requested for JSONArray but your server response as JSONObject. Try StringRequest for getting the JSONObject.
Try this:
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
// Loop through the array elements
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject js = jsonArray.getJSONObject(i);
Log.d("TESTING", js.getString("id"));
}
p.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("TESTING",error.getMessage());
}
})
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
Hope this will work.
You are request for json object but request time you call jsonarrayrequest so JsonException is come
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url,
null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject js = jsonArray.getJSONObject(i);
Log.d("TESTING",js.getString("id"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
});
I am trying to get JSON data from a URL like
www.exemple.com/json.php?apicall=cat
it works nice in the browser but it doesn't work in my android application.
here is my code :
request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
lstAnime.clear();
for (int i = 0; i < response.length(); i++) {
try {
jsonObject = response.getJSONObject(i);
Anime anime = new Anime();
anime.setId(jsonObject.getInt("id"));
anime.setName(jsonObject.getString("name"));
anime.setDescription(jsonObject.getString("description"));
anime.setImage_url(jsonObject.getString("path"));
lstAnime.add(anime);
} catch (JSONException e) {
e.printStackTrace();
}
}
setuprecyclerview(lstAnime);
mSwipRefresh.setRefreshing(false);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);
}
I am using volley library.
can anyone help me, please?
#Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);// you need to transform your response to object
arrayObject = new JSONArray(obj);//put the object in the jsonarray
for (int i = 0; i < arrayObject.length(); i++) {
JSONObject parObj = (JSONObject) parArray.get(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
I hope this helps you, Greetings!
This is my code and I am having error to parse data from object, what changes can be done so as to parse data...
aQuery.ajax(fetch_url, JSONObject.class, new AjaxCallback<JSONObject>(){
#Override
public void callback(String url, JSONObject obj, AjaxStatus status){
super.callback(url, obj, status);
Log.i("response", url + "response:" + obj);
ArrayList<UserInfo> list = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = jsonObject.getJSONArray("org_list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
UserInfo info = new UserInfo();
info.id = object.getString("Id");
info.servicename = object.getString("name");
info.amount = object.getString("amount");
list.add(info);
});
}
And this is my JSON data format
{
"org_list": [
{
"Id": "1",
"name": "CBC-Test",
"amount": "200"
}
]
}
When i edit from the below code and now i am facing null response value. I have also attached my logcat image file for further more details about my problem:Click here
Click here for further more details in my code
Logcat View1
Logcat View2
Edit your code as below,
aQuery.ajax(fetch_url, JSONObject.class, new AjaxCallback<JSONObject>() {
#Override
public void callback(String url, JSONObject obj, AjaxStatus status) {
super.callback(url, obj, status);
Log.i("response", url + "response:" + obj);
ArrayList<UserInfo> list = new ArrayList<>();
try {
JSONArray jsonArray = obj.getJSONArray("org_list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
UserInfo info = new UserInfo();
info.id = object.getString("Id");
info.servicename = object.getString("name");
info.amount = object.getString("amount");
list.add(info);
}
} catch (Exception e){
e.printStackTrace();
}
}
});
No need to re-create object of JsonObject. Just use that from response.
Finally I searched my self and found a solution. But it is without using AQuery and it is by using RequestQueue...
txtview = findViewById(R.id.showdata);
Button buttonParse = findViewById(R.id.showbtn);
requestQueue = Volley.newRequestQueue(this);
buttonParse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
jsonParse();
}
});
}
public void jsonParse(){
String fetchurl = "http://use your url here.com/";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, fetchurl, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("org_list");
for (int i=0; i < jsonArray.length(); i++){
JSONObject patient = jsonArray.getJSONObject(i);
String firstName = patient.getString("orga_organame");
txtview.append(firstName+","+"\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(request);
}