JSON request not responded - android

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);

Related

volley authentication errors when attempting to push to the server

I am developing an android app in which I have created a custom API for database. I am using volley for HTTP request. I am getting "com.android.volley.AuthFailureError" error on my onErrorResponse. I have checked the JSONObject in the debugger and it's valid.Moreover, I am using "application/json" in the header.
I am unable to find a way to fix this error. Here is the JsonRequest.
Here I am trying to use JSONRequest:
public void jsonPOST(String jsonInput) {
RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
String URL = "http://Servername:8080/users";
try{
JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(jsonInput),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response:%n %s", response.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
});
queue.add(req);
}
catch (JSONException error){
String output = error.toString();
}
}
I have checked the JSONObject in the debugger and it's valid. I am also checked the network firewall but it also seems fine

fetching data using json volley

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();
}
}

Multiple object request using Volley Android

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());
}
});

Unable to make jsonarrayrequest with volley

Im trying to make a post request with volley.
The request parameter is a json array .
Following is my request parameter
[
"Type",
{
"User": "email",
"password": "dimmer",
}
]
I have a method frameJsonArray that frames the above json and im making post request as follows,
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.POST, Constants.requestUrl, frameJsonArray(),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideDialog();
error.printStackTrace();
}
}
);
Im getting error in the above line of code where im making the request. How can I get this sorted?
Following is my error log
Error:(162, 46) error: constructor JsonArrayRequest in class JsonArrayRequest cannot be applied to given types;
required: String,Listener<JSONArray>,ErrorListener
found: int,String,JSONArray,<anonymous Listener<JSONObject>>,<anonymous ErrorListener>
reason: actual and formal argument lists differ in length
Following is my frameJsonArrayMethod
public JSONArray frameJsonArray() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("login_type", "Android");
jsonObject.put("username", email);
jsonObject.put("password", password);
jsonObject.put("short_name", null);
jsonObject.put("ip","123.421.12.21");
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jsonArray = new JSONArray();
jsonArray.put("Login");
jsonArray.put(jsonObject);
Log.d(TAG, "he;ll " + jsonArray.toString());
Toast.makeText(getApplicationContext(), jsonArray.toString(), Toast.LENGTH_SHORT).show();
return jsonArray;
}
JsonArrayRequest req = new JsonArrayRequest(url,
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();
}
});
Source : http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
once check your JSON , wrong format , you missed double quote.
[
"Type",
{
"User": "email /** you missed double quote here **/,
"password": "dimmer",
}
]
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.POST, Constants.requestUrl, frameJsonArray(),
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) {
hideDialog();
error.printStackTrace();
}
}
);
Change JSONObject to JSONArray in onResponse()
EDIT The error coming because there is only one constructor in JsonArrayRequest i.e.
public JsonArrayRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener){}
source https://android.googlesource.com/platform/frameworks/volley/+/e7cdf98078bc94a2e430d9edef7e9b01250765ac/src/com/android/volley/toolbox/JsonArrayRequest.java
you are using some constructor with 5 arguments.

Send image via JsonObjectRequest Volley in Android?

I have a POST method send a JsonObject via Volley:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, postBody, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
log("success");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
log("" + error);
}
});
request.setRetryPolicy(new DefaultRetryPolicy(10000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Volley.newRequestQueue(this).add(request);
With postBody:
JSONObject postBody = new JSONObject();
try {
postBody.put("name", "rome");
//postBody.put("image", file?);
} catch (JSONException e) {
log("" + e);
}
I want to send a image file with key "image" above. This file type is the same with file of form-data in PostMan (not url of file).
Is there anyway to do it? Thanks.

Categories

Resources