Error while compiling Android Volley code - android

i have this error while compiling this piece of code:
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
testtext.setText(response.toString());
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
testtext.setText(error.toString());
}
}
);
queue.add(getRequest);
The code is supposed to send get request from an url, and get JSON from it. But i'm getting an error while compiling it:
Error:(65, 86) error: incompatible types: String cannot be converted to JSONObject
Can somebody help me?

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.

Cannot cast 'com.android.volley.ServerError' to 'com.android.volley.NoConnectionError'

URL: http://androidtutorialpoint.com/api/volleyJsonObject
Code:
public void volleyJsonObjectRequest(String url) {
String REQUEST_TAG = "JSONOBJ_TAG";
JsonObjectRequest jsonObjectReq = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("VOLLEY RESPONSE", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
VolleyLog.d("VOLLEY ERROR", "Error: " + error.getMessage());
}
});
// Adding JsonObject request to request queue
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectReq, REQUEST_TAG);
}
I am trying to log the volley respone but I am getting the following error instead:
Cannot cast 'com.android.volley.ServerError' to 'com.android.volley.NoConnectionError'
I have checked the URL in the POSTMAN and it works fine. Is there something I am missing in my code? Been trying to debug but couldn't find the root cause.
Found the solution:
Replace http with https

Error:(54, 91) error: incompatible types: int cannot be converted to String

I'm trying to connect my app with server but getting an error. Please help!
I'm trying to connect my app using volley library to the server,but whenever I try to run my code I get the following error
incompatible types: int cannot be converted to String
I tried changing from String to int, but it didn't help!
show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, **<-------------error here**
showUrl, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray student = response.getJSONArray("student");
for (int i=0;i<student.length();i++){
JSONObject students = student.getJSONObject(i);
String firstname = students.getString("firstname");
String lastname = students.getString("lastname");
String age = students.getString("age");
result.append(firstname+""+lastname+""+age+""+"\n");
}
result.append("==\n");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
}
});
}
}
Got your problem now.
You're using incorrect method of JsonObjectRequest from Volley.
You're calling this method
JsonObjectRequest(String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener)
instead of
JsonObjectRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener)
where first argument is an int.
You're passing in 4 arguments, so the upper method is being called where your int method is being converted to String url
You can fix it by passing 5 arguments, and all should be good then.
requestQueue = Volley.newRequestQueue(this);
// follow this
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
"url", null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);

Android Volley: Unexpected response code 405

My Android Volley JsonObjectRequest runs into onErrorResponse with the issue:
BasicNetwork.performRequest: Unexpected response code 405 for MY_URL
My URL is valid. I have checked that with a browser and
I get there the expected JSON Object.
So the issue has to be on the client side.
The code 405 means:
Method Not Allowed The method specified in the Request-Line is not
allowed for the resource identified by the Request-URI. The response
MUST include an Allow header containing a list of valid methods for
the requested resource.
my code for JsonObjectRequest:
JsonObjectRequest jsonReq;
jsonReq = new JsonObjectRequest(URL_FEED, new JSONObject(),
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.v("ERROR:%n %s", error.getMessage());
}
});
// Adding request to volley request queue
NetworkController.getInstance().addToRequestQueue(jsonReq);
Do I have to add some information to the header? And if what for information?
The issue was that the request was set to POST by default.
The solution that worked for me:
JsonObjectRequest jsonReq = new JsonObjectRequest
(Request.Method.GET, URL_FEED, null, new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response)
{
Log.d("Server", "Läuft");
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error)
{
Log.d("Server","onErrorResponse");
}
});
NetworkController.getInstance().addToRequestQueue(jsonReq);
Use GET Method instead of POST it has worked for me.
I had the same issue, and I found out that my API URL was wrong.
So, my suggestion is to recheck the API URL.

JsonObjectRequest error

I'm getting an error for Request.Method on the "Method" part and it says "Cannot resolve symbol"and I don't know why. I'm trying to parse JSON to pull images from a website.
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, getRequestUrl(10),
(String) null, new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response)
{
L.zT(this, response.toString());
}
}, new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error)
{
}
});
You probably have the wrong import. Check in your import declaration of Request, and make sure that is
com.android.volley.Request
and not something else

Categories

Resources