I tried to update database on server with calling API by using Google Volley. But somehow the database won't updated. The issue doesn't exist while retrieving data (Method.GET)
Here my snipet code :
HashMap<String, String> params = new HashMap<>();
params.put(.....);
JsonObjectRequest postReq = new JsonObjectRequest(Request.Method.POST,
Api.URL_POST_DATA, new JSONObject(params),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
progressDialog.setVisibility(View.GONE);
if (response.toString().equalsIgnoreCase("{\"result\":\"OK\"}")) {
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "Check internet connection", Toast.LENGTH_SHORT).show();
}
});
postReq.setRetryPolicy(new DefaultRetryPolicy(10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
postReq.setShouldCache(false);
VolleyHelper.getInstance(this).addToRequestQueue(postReq);
VolleyHelper.getInstance(this).getRequestQueue().getCache().invalidate(Api.URL_POST_DATA, true);
Then i tried update database manually by using PostMan to make sure that the problem not on my API side and my database successfully updated.
Did i doing wrong on my code ? Any helps will be really appreciated.
Thanks
Set the Content-Type value of your request header to "application/json". This may be the issue.
In order to do that, you need to override the getHeaders() method on your request object.
Thanks guys for the clue, i did wrong. I sent the HashMap but on my request i sent the JsonObjectRequest. So i used StringObject instead of JsonObjectRequest.
Related
I am trying to fetch a JSON response from OpenWeatherAPI to incorporate the current weather in my app. I have used volley to make a simple request to fetch the JSON response, but every time, I do not get the response. Instead, it always triggers the onErrorResponse method. What do I change to make this work?
I have added the uses Internet Permission in my manifest.
I have tried the solution to fetch JSON responses from many sources including the Official Android Developers Documentation, Other Questions from Stack Overflow, etc, but all of them failed.
I first used JSONObjectRequest instead of StringRequest, but even that did not provide me the results I required.
/*
Create a request queue to fetch the JSONObject response.
*/
RequestQueue queue = Volley.newRequestQueue(Objects.requireNonNull(getContext()));
/*
JSON Object request.
*/
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "No JSON", Toast.LENGTH_SHORT).show();
}
});
queue.add(request);
I expect the Toast to show the response, but The Toast shows "No JSON".
Add "https://" before your URL. You will get a response on your browser, but will not in Android.
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 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);
I have url that has a json Array which contains a large amount of data.
I call the url with volly jsonArrayRequest like this
public void makeJsonArrayReq(){
showProgressDialog();
JsonArrayRequest req = new JsonArrayRequest(Const.URL_IPD_ADMITED,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d("response ================", response.toString());
textView.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error", "Error: " + error.getMessage());
textView.setText("Error Occurs ");
hideProgressDialog();
}
});
AppController.getInstance().addToRequestQueue(req, "array");
}
My devise shows progressDialog for some time after that time it hang out the app then after a few minute it shows the response in the texview but it very lengthy process and it same when internet connection off and extract from volley cache. How can I handle the URL in my apps?
You need to change your api a bit to support pagination. There are several pagination techniques available, you have to choose one that suites your case.
In Android you can save the last fetched pageNumber(if that is what your API returns as page identifier) and your api should accept this variable in request (mostly through query params)
http://43.255.22.123:3000/android/mis/get/ipdAdmitPatMd?pageId=1
And in the next request it should update the pageId to 2.
Since the question is too broad hence providing exact code solution is not possible therefore I have explained the concept.
I am trying to POST data to an MVC WebApi using "Volley Library".
You can find the api details here:
http://mzrokz.somee.com/Help/Api/POST-api-LeadNowAppToCore-AddCustomer_firstname
It will return "1" if success. But right now when i am trying to post it returns this exception
BasicNetwork.performRequest: Unexpected response code 404.
This is what I have tried uptill now.
RequestQueue queue = Volley.newRequestQueue(LeadNowAddUser.this);
StringRequest sr = new StringRequest(Request.Method.POST,"http://mzrokz.somee.com/api/LeadNowAppToCore/AddCustomer", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("response",response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("firstName","ankur");
return params;
}
};
queue.add(sr);
Please help me. Thanx in Advance.
I try post data in extension Advanced Rest client of chorme Browser but it error 404.
Please check your webservice
http://i.imgur.com/z70z4km.png
According to the documentation in your link, this is the POST request with no body parameter. It uses URL parameter, so you only need to update the URL in your app and remove getParams, please see the following screenshot (I use Postman). Hope this helps!