How to get JSON from server without send JSON request - android

I need help.
I use volley for sending json object to my rest API server. And i get data from this API to my app (json). Its work fine:
JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, JSONDATA, JSONListener, errorListener)
...
And now I want to send request without JSONDATA (i can't set null). It is for global values. There is not necessary send some data. And i dunno how to send this request. Can you help me?

till i understand ur problem my answer is this
StringRequest distRequest=new StringRequest(Request.Method.POST, YOUR_URL, new Response.Listener<String>() {
#Override public void onResponse(String response) {
Toast.makeText(MainActivity.this, " "+response.toString, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, " "+error.toString, Toast.LENGTH_SHORT).show();
}
});
RequestQueue distQueue=Volley.newRequestQueue(this);
distQueue.add(distRequest);
}

Try this:
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
queue.add(getRequest);

Related

Volly is giving me com.android.volley.AuthFailureError in Android

CODE
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(MainActivity.this, "working", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
}
});
queue.add(jsonObjectRequest);
PROBLEM
This is my code in which I'm using the Volly-Library to get the JSON Object using the GET Method. I'm using Volly Library in my Android App to get the Json response using the Volly-Library. But it is throwing the AuthFailureError.

volley code onResponse() and onErrorResponse() are not called

Here is my volley code, but onResponse() as well as onErrorResponse() are not called. The Log lines are never printed as these metods are skipped. But the last log line appears in logcat and the jsObjRequest is always null.
String url = "A valid url";
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.e("onResponse",""+response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("onErrorResponse", "" + error);
}
});
Log.e("jsObjRequest", "" + jsObjRequest);
You need to enqueue your request:
RequestQueue queue = Volley.newRequestQueue(this);
String url = "A valid url";
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.e("onResponse",""+response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("onErrorResponse", "" + error);
}
});
Log.e("jsObjRequest", "" + jsObjRequest);
queue.add(jsObjRequest);
see this: https://developer.android.com/training/volley/simple.html
You should add your request to a RequestQueue. In your situation simply add these 2 lines of code below yours, it will work then.
RequestQueue rQueue = Volley.newRequestQueue(this);
rQueue.add(jsObjRequest);
For this issue i use HTTPS and its working very well now

Volley sends null parameters to server

I'm trying to send JSON parameter but server is receiving them as nulls values,
i tried to request it from Postman and it works perfect, i don't know what the problem is with volley i followed the instructions here but it didn't make sense
here is my code
String url = "http://10.10.10.166:8080/SystemManagement/api/Profile/Login";
JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("user_id","Test user name");
jsonObject.put("user_password","test password");
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(jsonObject.toString());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.toString());
}
});
//add request to queue
queue.add(jsonObjectRequest);
I faced same problem, solved by overriding the getParams() method
Here is my login request using Volley.
private void loginRequeset() {
showpDialog();
StringRequest jsonObjReq = new StringRequest(Request.Method.POST, Constants.LOGIN_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
hidepDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
})
{
#Override
protected Map<String, String> getParams() {
Map<String, String> signup_param = new HashMap<String, String>();
signup_param.put(Constants.USERNAME, _emailText.getText().toString().trim());
signup_param.put(Constants.PASSWORD, _passwordText.getText().toString().trim());
return signup_param;
}
};
// Adding request to request queue
queue.getInstance().addToRequestQueue(jsonObjReq);
}

How to make a Json Array Request with Request Parameters

I have to make json array request with the following request paramatetrs.
[
"Login",
{
"password": "",
"username": "",
"ip": "12.123.124.12",
"login_type": "Android"
}
]
I use volley to make post request. In volley, if we have to make jsonarrayrequest, we do something like the following,
JsonArrayRequest req = new JsonArrayRequest(Constants.requestUrl,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
The problem is that, How can I ever be able to insert my request parameters in the above code snippet. In case of JsonObjectRequest, we have provision to insert as follows,
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Constants.requestUrl, frameLoginJson(),
new Response.
Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideDialog();
error.printStackTrace();
}
}
);
In frameLoginJson, im framing the request parameter and dispatching the request.
But Im unable to do the same in case of JSONArrayRequest. How can I be able to make json array request with request parameters using volley especially or by any other mean?
You would need to override the getParams method with the following signature: protected Map<String,String> getParams().
Here's an example:
see the answer to Volley not calling getParams() for standard POST request

volley returns error response

I have to make a json post request with the following parameters.
{"method":"login","data":{"username":"korea","password":"123456"}}
I use volley to make post request and follwoing is my code to do post request.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, loginURL, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(mContext,response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<String,String>();
map.put("method","login");
map.put("username","korea");
map.put("password","123456");
return map;
}
};
requestQueue.add(jsonObjectRequest);
requestQueue.start();
Im getting error response from server. How to get proper response from server?
Change Request.Method.GET to Request.Method.POST. Then pass a JSONObject as the third parameter where you currently have null;
For example:
JSONObject data = new JSONObject();
data.put("username","korea");
data.put("password","123456");
JSONObject jsonObject = new JSONObject();
jsonObject.put("method","login");
jsonObject.put("data",data);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, loginURL, jsonObject, responseListener, errorListener);
I use volley to make post request and follwoing is my code to do post request
That is not a POST request, AFAICT. You are using Request.Method.GET.
Use following method to post the data to server
public void postDataVolley(Context context,String url,JSONObject sendObj){
try {
RequestQueue queue = Volley.newRequestQueue(context);
JsonObjectRequest jsonObj = new JsonObjectRequest(url,sendObj, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("Volley", "Volley JSON post" + response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Volley", "Volley JSON post" + "That didn't work!");
}
});
queue.add(jsonObj);
}catch(Exception e){
}
}

Categories

Resources