I am fetching data from db and want to set text. Values are getting fetched and displaying in url but setText not working, when I used JSONObject also same problem. Where am I going wrong.? Below is my code and backend output.
{
"status": 200,
"db": {
"test_count": 2539,
"franchise_count": 2,
"patient_count": 1,
"invoice_count": 1,
"total_income": "12140",
"current_income": "12140",
"total_expense": null,
"current_expense": null,
"user_count": 2
}}
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
progressDialog.dismiss();
try {
JSONObject object = new JSONObject();
u = object.getString("user_count");
user_count.setText(u);
} catch (JSONException e) {
Toast.makeText(getContext(),"No Records Found",Toast.LENGTH_LONG);
Log.e("Error", "Failed" +e.toString());
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Log.e("Error", "Try Later" +error.toString());
Toast.makeText(getContext(),"No Records Found",Toast.LENGTH_LONG);
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(objectRequest);
}
Change your try block contents like below
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
progressDialog.dismiss();
try {
JSONObject newObject=response.getJSONObject("db");
u = newObject.getString("user_count");
user_count.setText(u);
} catch (JSONException e) {
Toast.makeText(getContext(),"No Records Found",Toast.LENGTH_LONG);
Log.e("Error", "Failed" +e.toString());
e.printStackTrace();
}
}
Related
I'm trying to retrieve a token from a JSON response. I've successfully managed to get a response, but I don't know how to retrieve the token.
Here's my code
private void TmdbAuth() {
final String TokenUrl = "https://api.themoviedb.org/3/authentication/token/new?api_key=<<api key goes here>>";
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
TokenUrl,
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
Log.i("jjj", response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}
);
requestQueue.add(jsonObjectRequest);
}
and the response of this json
{"success":true,"expires_at":"2019-04-01 10:49:44 UTC","request_token":"request token goes here"}
How do I retrieve only the request_token
Try this #tony
JsonObject object = new JsonObject(response); //obtained response from the server.
String request_token = object.getString("request_token");
Log.e("request_token",request_token); //Just for checking in the logcat.
You can access token String this way
String token = response.optString("request_token");
You can try like this
try{
Log.i("jjj", response.toString());
String requestToken = response.getString("request_token");
}
catch (Exception e) {
e.printStackTrace();
}
I am using Volley to make Api call and set data to RecyclerView.I have my first Volley where I get Id field and I use this same Id to make another Api call. I used the response from both api call at same time and set the data to RecyclerView.But the response from second Api call is not set on Recyclerview at same time.Later I manually refresh tab and set data from Database its set.
This is my nested Volley Api call
private void getData() {
final JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, ApiUrls.FirstCall, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
if (response != null) {
for (int i = 0; i < response.length(); i++) {
JSONObject object = response.getJSONObject(i);
CurrentStatusEntry entry = new CurrentStatusEntry();
String Id= object.getString("id");
id_url = ApiUrls.Info+ Id + "/";
JsonObjectRequest foodie_request = new JsonObjectRequest(Request.Method.GET, id_url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
String name=response.getString("name");
String address=response.getString("address");
entry.setName(name);
entry.setAddress(address);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
})
AppController.getInstance().addToRequestQueue(foodie_request, foodie_data_req);
current.add(entry);
if (current.size() > 0) {
adapter = new QueueAdapter(current, getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}
})
AppController.getInstance().addToRequestQueue(request, restqueue_req);
}
The data from second api call is not set instantly. How to perform this ?
I have a webservice which returns json files. If I call this url (http://192.168.178.67:8080/simplestock/webapi/swipeService/swipes/Aktien) on my smartphone browser, I will get a json array back.
Now I've tried to get this json in my android project with this code:
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d("Success", "onResponse is Called");
try {
Log.d("Get Object: ", response.getJSONArray(1).toString());
} catch (JSONException e) {
e.printStackTrace();
Log.d("Failure", "JSON Error");
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("JSON", "Error");
}
});
The problem is that the respond isn't called and I don't get any error message. I've added the internet permission in the manifest. I test on my smartphone, which is in the same network than the localhost. Anybody got an idea?
In what ever method you are calling your code you need to add the request to the queue: jsonObjectRequest.add(jsonArrayRequest);
Like this:
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d("Success", "onResponse is Called");
try {
Log.d("Get Object: ", response.getJSONArray(1).toString());
} catch (JSONException e) {
e.printStackTrace();
Log.d("Failure", "JSON Error");
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("JSON", "Error");
}
});
jsonObjectRequest.add(jsonArrayRequest);
On the below code i want to use Response but it throws JSONException
StringRequest strReq = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.v(TAG, "Response: " + response);//output {"error":false}
try {
JSONObject jo = new JSONObject(response); // java.lang.String cannot be converted to JSONObject
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
Sample JSON string:
{
"error": false,
"uid": "5b081af13eb974.69226352",
"user": {
"name": "66699",
"created_at": "2018-05-25 18:47:21"
}
}
How i should solve this?
Well, to retrieve the content of your response you don't need to use JSON while using POST request. Instead do something like this :
if (response.equalsIgnoreCase("yourResponseFromTheWebService")) {
...
Toast
} else if (response.equalsIgnoreCase("OtherPossibleResponse")){
....
Toast
} else{
....
Toast
}
in case of GET request you should do something like this :
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, URL, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// display response
try {
//put response into string
JSONArray Jresult = response.getJSONArray("user");
for (int i = 0; i < Jresult.length(); i++) {
JSONObject json_data = Jresult.getJSONObject(i);
String lName = json_data.getString("name");
String lCreatedAt = json_data.getString("created_at");
//create a model class with your user info like name and created_at and for every user found create a new user object and store its info.
_myUser.add(new User(lName, lCreatedAt));
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("Response", response.toString());
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
}
}
);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(getRequest);
I want to call API Using Volley. There is an Multiple object Request So i don't Know Proper way. I was tried to code as below.But it does not give me Response.So can Anyone help me???
MainActivity
public void getJsonResponsePost(){
JSONObject jsonData = new JSONObject();
JSONObject json = new JSONObject();
/*{"data":{"lang_type":"1","keyword":"","latitude":23.022499999999997,"longitude":72.57139833333333,"category":6}}*/
try {
jsonData.put("data",json);
json.put("lang_type","1");
json.put("keyword","");
json.put("latitude",23.022499999999997);
json.put("longitude",72.57139833333333);
json.put("category",6);
Log.d("TAG",jsonData.toString());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, json, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response)
{
Log.d("String Response :",response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(" Error getting :",error.toString());
}
});
jsonObjectRequest.setTag(REQ_TAG);
requestQueue.add(jsonObjectRequest);
}
I get the following response
StringĀ ResponseĀ :: {"status":"0","message":"Please pass the language type."}
Can you try this?
try {
json.put("lang_type","1");
json.put("keyword","");
json.put("latitude",23.022499999999997);
json.put("longitude",72.57139833333333);
json.put("category",6);
jsonData.put("data",json);
Log.d("TAG",jsonData.toString());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonData, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response)
{
Log.d("String Response :",response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(" Error getting :",error.toString());
}
});