I am using Volley in my android client to retrieve some JSON values from server.
To get Value I did the following :
#Override
public void onClick(View v) {
queue = Volley.newRequestQueue(MapsActivity.this);
request = new JsonObjectRequest(
Request.Method.GET, // the request method
"http://10.0.2.2:8080/SomeURL",
new JSONObject(map),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response){
textView = (TextView)findViewById(R.id.textView);
textView.setText(response.toString());
}
},
new Response.ErrorListener() { // the error listener
#Override
public void onErrorResponse(VolleyError error) {
textView = (TextView)findViewById(R.id.textView);
textView.setText("Error::"+ error.toString());
Toast.makeText(getApplicationContext(), "Error:" + error.toString(), Toast.LENGTH_LONG).show();
}
});
queue.add(request);
}
});
And In server side I am returning the value as following:
#GET
#Produces("application/json")
public List<Employee> getEmployees() {
EmployeeDAO dao = new EmployeeDAO();
List employees = dao.getEmployees();
return employees;
}
However, I am getting the values in "onErrorResponse()"
You are waiting for a JSONObject in your code, change JsonObjectRequest with JsonArrayrequest. In onResponse(JSONArray response) you parse the response
Related
here is my code, whenever i try to make the Response to convert to toString() i get the error
private void fetchStoreItems() {
String url = "https://newsapi.org/v1/articles?source=techcrunch&apiKey=47bdfe44632849b4bdf0c2a9035a68e7";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
url, new com.android.volley.Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
VolleyLog.d("ecardCalled: ", response.toString());
}
},new com.android.volley.Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("ecardCalled: ", error.toString());
}
});
MyApplication.getInstance().addToRequestQueue(jsonArrayRequest);
}
what am i not actually doing Right?
You have to provide json params value if you have any otherwise set it to null
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
url,null, new com.android.volley.Response.Listener<JSONArray>() {
#Override
....
....
...
//other code
use this code instead of yours:
private void fetchStoreItems() {
String url = "https://newsapi.org/v1/articles?source=techcrunch&apiKey=47bdfe44632849b4bdf0c2a9035a68e7";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
VolleyLog.d("ecardCalled: ", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("ecardCalled: ", error.toString());
}
});
MyApplication.getInstance().addToRequestQueue(jsonArrayRequest);
}
I am creating an app which uses volley. I used JsonObjectRequest() to send Json object through volley. Thus I had to create Jason Object from values taken from Edit text.
btn_enter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SN1 = inputsn.getText().toString();
Name1 = inputname.getText().toString();
}
});
JSONObject jsonMenu= null;
try {
jsonMenu = new JSONObject("");//string is to be added here
Toast.makeText(Add.this,"Made Obj",Toast.LENGTH_SHORT);
} catch (JSONException e) {
e.printStackTrace();
Log.d("Add","Error");
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL,jsonMenu, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("MainActivity",response.toString());
Toast.makeText(Add.this,"Response Received",Toast.LENGTH_SHORT);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Add.this,"Error",Toast.LENGTH_SHORT);
}
});
RequestQueue queue= Volley.newRequestQueue(this);
queue.add(request);
Here, [{"SN":1,"Name":"Ajeeb"}] values 1 and Ajeeb is to be replaced by values of SN1 and Name1 respectively.Such that I can add it to Java code
JSONObject jsonMenu = new JSONObject("\\String goes here");
You can create property for json object and than as it to your Json Object
jsonMenu.addProperty("SN", SN1);
jsonMenu.addProperty("Name", Name);
This will result in {"SN":1,"Name":"Ajeeb"}
And if you want to create an array for it
JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonMenu);
This will result in [{"SN":1,"Name":"Ajeeb"}]
im pretty new to Android Studio and I'm trying to build a Get Request using Volley, the Server response is a JsonObject. I tested the code with breakpoints but I wonder why I don't jump into onResponse or why it won't work.
Here's my Code of the Get Method:
public Account GetJsonObject(String urlExtension, String name, Context context) {
String baseURL = "myurl.com/api";
baseURL += urlExtension + "/" + name;
// url will look like this: myurl.com/api/user/"username"
final Account user = new Account("","");
//Account(name, email)
RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(context);
JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null,
new Response.Listener<JSONObject>() {
// Takes the response from the JSON request
#Override
public void onResponse(JSONObject response) {
try {
JSONObject obj = response.getJSONObject("userObject");
String username = obj.getString("Username");
String email = obj.getString("Email");
user.setUsername(username);
user.setEmail(email);
}
catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObject);
return user;
}
As #GVillavani82 commented your onErrorResponse() method body is empty. Try to log the error like this
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("ERROR", "Error occurred ", error);
}
}
Make sure that you have the below permission set in AndroidManifest.xml file and the api URL is proper.
<uses-permission android:name="android.permission.INTERNET"/>
And JsonObjectRequest class returns Asynchronous network call class. Modify your code like below.
// remove Account return type and use void
public void GetJsonObject(String urlExtension, String name, Context context) {
....
.... // other stuffs
....
JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null,
new Response.Listener<JSONObject>() {
// Takes the response from the JSON request
#Override
public void onResponse(JSONObject response) {
processResponse(response); // call to method
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("ERROR", "Error occurred ", error);
}
});
requestQueue.add(jsonObject);
}
Now create another method like below
private void processResponse(JSONObject response) {
try {
final Account user = new Account("","");
JSONObject obj = response.getJSONObject("userObject");
String username = obj.getString("Username");
String email = obj.getString("Email");
user.setUsername(username);
user.setEmail(email);
} catch (JSONException e) {
e.printStackTrace();
}
}
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);
Hello friends I have following JSON Array response from a web service and I want to read all the data row wise from the Database and display on Android Activity.
[{"id":"2","type":"0","title":"Recruitment at ADANI Port, Mundra","date":"2016-07-01"},{"id":"1","type":"1","title":"Training at DAICT, Gandhinager","date":"2016-07-04"}]
I have implemented following code in onCreate() of an Activity using Volley Library, which doesn't show output and to my knowledge it doesn't call onResponse() method.
public void getNews(){
String url = "http://www.ABCDXYZ.net/tponews.php?tponews=1";
Log.e("URL",url);
requestQueue = Volley.newRequestQueue(this);
JsonArrayRequest jor = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
Log.e("Resposne", "We have got the response");
JSONObject val = response.getJSONObject(0);
}catch (JSONException e){e.printStackTrace();}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley",error.toString());
}
}
);
requestQueue.add(jor);
}
The output is only URL in Log.e("URL",url); // URL: www.ABCXYZ.net/tponews.php?tponews=1