I want to use Volley to get a JSON response from some website, so I started testing it. Here is my code plain and simple:
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, "http://api.androidhive.info/volley/person_object.json", null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
json = response;
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
error.printStackTrace();
}
});
int x = json.length();
After the request is made the response is always null. Neither an error is raised neither the request succeeds. Which is really confusing. As you can see I am assigning the value of the response to a variable named json which is of the same type. When I debug the application by putting a breakpoint on the onResponse method, onErrorResponse method and on the last line, the debugger only hits the last line the variables watches indicate that the value of the response is null.
I have tried more than one URL
http://simplifiedcoding.16mb.com/UserRegistration/json.php
https://androidtutorialpoint.com/api/volleyString
I have added Volley via gradle
compile 'com.android.volley:volley:1.0.0'
Put it in a RequestQueue
RequestQueue queue = Volley.newRequestQueue(this);
Then
queue.add(jsObjRequest);
Or
ApplicationController.getInstance().addToRequestQueue(jsObjRequest);
You need to add the request to the queue for the asynchronous request to work.
RequestQueue requestQueue= Volley.newRequestQueue(this)
requestQueue.add(jsObjRequest);
Related
I am making Volley GET Requests which will work until I get to a specific activity and then it stops calling the onResponse in the Response.Listener. Here is my Volley Request method:
public static void stringReqGet(String url, final VolleyCallback callback){
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("In VolleyRequest onResponse(): " + response);
callback.onSuccess(response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error" ,error.getMessage());
}
});
System.out.println("Adding request to queue: " + stringRequest.toString());
Utilities.getInstance().addToRequestQueue(stringRequest);
}
Here's how I'm making the request in the activity in question:
CourseUtil.loadCourse(courseID, new VolleyCallback() {
#Override
public void onSuccess(String response) {
System.out.println("JSON Success");
courseJSON = response;
}
});
and in CourseUtil class:
public static void loadCourse(int courseID, VolleyCallback callback){
VolleyRequest.stringReqGet(Const.URL_COURSE + courseID,callback);
}
I'm getting no Volley errors just seemingly no response. Here are my (truncated) logs:
I/System.out: Adding request to queue: [ ] http://server/courses/267267267 0xebb503b0 NORMAL null
I/System.out: In VolleyRequest onResponse(): *Correct Response Object*
.
.
.
I/System.out: 267267267 <-- Same course ID used in first request
I/System.out: Adding request to queue: [ ] http://server/courses/267267267 0xebb503b0 NORMAL null
*onResponse never called*
The first time this request is made you can see that the onResponse() method is called and it returns the correct object. The signature of this 'correct' request is the exact same as the one above. The next time that exact request is made the onResponse is never called and I get no error.
I've looked at other posts on here but none of their solutions fixed the problem I am having and no error makes it hard to know what's going on.
Any help would be greatly appreciated.
Put:
System.out.println("JSON Success");
courseJSON = response;
into a separate method outside the callback and it works now. Not sure why...
Here is my code to ignore errors:
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError ignored) {
//nothing
}
});
But still it returns 403 error
E/Volley: [934] BasicNetwork.performRequest: Unexpected response code 403 for http://myawesome.site/login
E/Volley: [934] BasicNetwork.performRequest: Unexpected response code 403 for http://myawesome.site/login
Another problem is: above error shows twice in one call. It ignores DefaultRetryPolicy set to 0
Expecting your help.
To stop logging network activity, call the DevicePolicyManager method setNetworkLoggingEnabled() and pass false as the enabled argument.
Your DPC can call isNetworkLoggingEnabled() to check if network activity is currently logged.
reference link :https://developer.android.com/work/dpc/logging.html
check this section : Enable network logging.
I have solve problem by changing the method name. previously i am try to calling web service with POST method and I have changed to GET method then my problem solved. Try this solution by changing request method.
StringRequest stringRequest = new StringRequest(Request.Method.GET, "YOUR_WEBSERVICE_URL",
new Response.Listener<String>() {
#Override
public void onResponse(String response){}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {}
}) {
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
return params;
}
};
stringRequest.setRetryPolicy(new
DefaultRetryPolicy(60000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
I have a little app with a leaderboard and i want to hide players with fake scores. I read about it at https://developers.google.com/games/services/management/api/players/hide#request
The Problem is, that i have no idea from http Requests and that things.
So how do i send a HTTP Request? Is there a Terminal or something in the Developer Console from Google, where i put my command in?
Or what do i need to do, to send an Request like this?
I recommend that you use Volley
Add Volley to your project through Gradle
compile 'com.android.volley:volley:1.0.0'
Add the android.permission.INTERNET permission to your app's manifest.
The code is taken from 1
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com"; //set your web call here
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//handle success
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//handle error
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
I use volley in the Android Activity, and make a request and got the response, but I want to handle the response maybe in an another method,but it won't work, what should i do ?
public class TestActivity extends Activity {
RequestQueue queue;
private String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "www.google.com/something/I/need";
queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Do something with the response
Log.i("resp", response);
// I want to do sth with the response out of here
// maybe like this, let result = response
// and see the log at the end of the code
// but it failed, what should I do?
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Handle error
Log.e("error", error.toString());
}
});
queue.add(stringRequest);
Log.e("result", result);
}
The Volley requests are asynchronous, so the program after sending the request, continues execution without waiting for the answer. So the code that processes the result is inserted into the OnResponse method. For more precise help explain why you would like to log out of the method OnResponse
Think about what you're doing: You're creating a StringRequest, then you add it to the request queue, but then you immediately try to check the result. Obviously, this won't work because the request hasn't been processed yet.
Your response will arrive in the onResponse method, and only then you'll be able to do something with it. You can set result = response here, but you'll only be able to see the value when the onResponse is called which could take some time.
Hope this clarifies things.
I'm making a volley request in this way:
public void makeRequest(BaseRequest request, Response.Listener<JSONObject> responseListener,
Response.ErrorListener errorListener) {
if (Constants.DEBUG) Log.i(TAG, "Sending: " + request.getUrlRequest());
JsonObjectRequest jsObjRequest = new JsonObjectRequest(METHOD, request.getUrlRequest(), null, responseListener, errorListener);
// disable cache
jsObjRequest.setShouldCache(false);
jsObjRequest.setTag(mTag);
mQueue.add(jsObjRequest);
}
mTag is a Class type. I have an activity where on its onCreate method I call the volley request with this:
mVolleyManager.makeRequest(getRequest(), new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
refreshLayout.setRefreshing(false);
onEndLoading(response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
refreshLayout.setRefreshing(false);
onErrorLoading(error);
}
});
If I start open and closing the activity for a bunch of time, my memory keeps growing till it reaches an OOM error. I tried to have a look with MAT and here's the result:
It seems that Volley keeps in memory all of its requests, even if the onResponse method is correctly called. I already solved the problem by switching to Retrofit. Same code, same requests and it's working but I want to understand what could be the cause of my problem.
NetworkDispatcher has only 4 threads by default so i think your problem is caused by creating multiple requestqueue and old threads dose not killed, use singleton queue, for refering to that look at:
http://developer.android.com/training/volley/requestqueue.html
each request queue has only 4 threads but in your image i can see more than 4 so obviously your problem caused by not using singleton design pattern.