android volley basic request wont work for Special WebSite - android

Volley:
try to get respone from a url with Volley Lib in android
its ok with google.com or other site but this website return error
url = http://nobat.larums.ac.ir/QueueWeb/Home/Login
My Code:
RequestQueue queue = Volley.newRequestQueue(getActivity());
String url ="http://nobat.larums.ac.ir/QueueWeb/Home/Login";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
textView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
textView.setText("That didn't work!");
Log.d("Volley error: ",error.toString());
}
});
queue.add(stringRequest);
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
Logcat:
[OkHttp] sendRequest>>
I/System.out: [OkHttp] sendRequest<<
D/Volley error:: com.android.volley.NoConnectionError: java.net.ProtocolException: Too many follow-up requests: 21
Jsoup:
Try Connect url with Jsoup lib and good work.
Document htmlDocument = Jsoup.connect("http://nobat.larums.ac.ir/QueueWeb/Home/Login").get();
Whats the problem with this site?

Related

ESP32 Android App communication over HTTP

I have been trying to connect an ESP32 to my Android app using SoftAP and Webserver in ESP32 and Volley Library in android. I am able to connect to the ESP32 webserver from a web browser. But I am not able to access the webpage through android volley. I am getting a null error response in volley.
My phone is able to connect to the SoftAP.
Here is the ESP32 code of the server:
void setup()
{
pinMode(pin_led, OUTPUT);
Serial.begin(115200);
SPIFFS.begin();
wifiConnect();
server.on("/l",[](){server.send_P(200,"text/html", webpage);});
server.on("/toggle",toggleLED);
server.on("/settings", HTTP_POST, handleSettingsUpdate);
server.begin();
}
Here is my volley request:
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
String url ="http://192.168.4.1/l";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
Log.d("Connection", "Response: "+response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
try{
Log.d("Connection", error.networkResponse.toString());
} catch (Exception e){
Log.d("Connection", e.toString());
}
}
});
// Add the request to the RequestQueue.
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
1,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(stringRequest);
It seemed the issue was because "Cleartext Traffic" was not enabled by default in Oreo. Once I added the following code in the manifest it was working.
<activity android:name=".MainActivity" android:usesCleartextTraffic="true">
The solution was found in detail from this StackOverflow answer to a similar issue.

Why Volley response is received as "[" and not JSON

I am learning about Volley and I don't know why the response from GET method is coming as a single char -> [.
I am using this method to get the JSON response:
public void getJsonMethod() {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
// String url = "https://www.w3schools.com/js/myTutorials.txt";
String url = "http://www.google.com"; // with this url I am getting response
// Request a string response from the provided URL.
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("Response is: " + response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println("Response is not good" + error.getMessage());
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
When I am using this link I do get a response but when I try to use some link that contains nothing but JSON like this one my response it "[".
I am calling this method from Activity like this:
GetJsonClass getJson = new GetJsonClass(this);
getJson.getJsonMethod();
Any ideas on what am I doing wrong here?
Answer + code
If anyone will start using Volley maybe this can help him :
as David Lacroix said in his answer, I called stringRequest and notJsonArrayRequest.
Here is how it should have been:
public void getJsonMethod() {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
String url = "your url";
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
System.out.println("this is response good" + response);
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println("this is response bad" + error);
}
});
queue.add(jsonObjectRequest);
}
See https://developer.android.com/training/volley/request
StringRequest. Specify a URL and receive a raw string in response. See Setting Up a Request Queue for an example.
JsonObjectRequest and JsonArrayRequest (both subclasses of JsonRequest). Specify a URL and get a JSON object or array (respectively) in response.
You should be using a JsonArrayRequest
myTutorials.txt is being served with status code 304 (no proper suffix and MIME type either):
304 Not Modified. If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD respond with this status code. The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.
In other terms, what the browser may display is not neccessarily the same what the server has sent. eg. GSON would accept that JSON only with option lenient enabled, because the array has no name.
see RFC 2616.

Unexpected response code 307 for GET request for local service

I'm trying to connect to a localhost web service that I wrote in .net core 2.0. Using the emulator's browser I was able to invoke it and get JSON data back. However, inside my app when I try to get it I'm getting response code 307 back.
public void onClick(View view)
{
String url = String.format("http://10.0.2.2:5000/api/testservice/tickerpnl?ticker=%s&purchaseDate=%s&shares=%s/"
, tickerText.getText().toString(), dateText.getText().toString(), sharesText.getText().toString());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try
{
double pnl = response.getDouble("PNL");
} catch (JSONException e)
{
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(jsonObjectRequest);
}
On the stacktrace I'm getting the following:
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
E/Volley: [403] BasicNetwork.performRequest: Unexpected response code 307 for http://10.0.2.2:5000/api/testservice/tickerpnl?ticker=AAPL&purchaseDate=1-1-15&shares=1
Again using the link from the stack trace I was able to get the JSON data back just pasting it into the emulator's browser but somehow it just won't work using JsonObjectRequest.
the requestqueue was initialize as
private RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(this);
I found out the issue was that for .net core I had code that redirect http to https so that's why I was getting the 307 error since I was trying to call it using http

Parse url with curly brackets in Android App

How can i Parse url with curly brackets like
http://example.com/api/login/{username}/{password} in an android application.
Normal volley post request returns html.But i need JSON.
Integrating Login API in Android App
If I get you right, what you want to do is send a GET request for login.
The following code can help you (it is not recommended to use GET for login, use POST instead. I'm giving a GET ex because that's what your'e asking for):
final TextView mTextView = (TextView) findViewById(R.id.text);
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://example.com/api/login/your_username/your_password";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});

Android Volley no Response from StringRequest

(Sorry for any english mistake its not my native language)
I'm trying to parse html using Volley and Jsoup.
while debugging / running the app, the StringRequest dosent invoke onResponse() or onErrorResponse(), basically there no response or Error from the Response.Listener (i guess).
so i cant "begin" parsing the html because the onResponse() is never invoked.
Ive searched for answer and nothing seems to get it fixed.
Could it be that the RequestQueue cache kicks in and its not trying to get the response? (just brain storming tried so many things).
--Im just trying to retrive data from html, so i could show the updated data from the url in my app, is Volly the way or should i use simpler mathods?
here is my volley StringRequest and RequestQueue:
String url = "http://www.fringeb7.co.il/";
final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
response_utf8 = URLDecoder.decode(URLEncoder.encode(response, "iso8859-1"),"UTF-8");
doc = Jsoup.parse(response_utf8);
Log.d("logr=",response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("log2=", error.toString());
requestQueue.stop();
}
});
// Add the request to the RequestQueue.
requestQueue.add(stringRequest);
took me long enough, but there was no problem.
the code works, while debugging there were no response,
but when i run the app the request complete and i got the wanted response.
so if anyone have a similier problem, from some reason while debugging I dident get response but while running the app its working fine.
(simpley dident know that)
try this first before wasting time in debugging like i did.
If you use Jsoup, you can get it and parse in very simple way:
Document doc = Jsoup.connect("http://www.fringeb7.co.il/").get();
If you still want to use Volley, below code works perfectly in my device:
String url = "http://www.fringeb7.co.il/";
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
String response_utf8 = URLDecoder.decode(URLEncoder.encode(response, "iso8859-1"), "UTF-8");
Document doc = Jsoup.parse(response_utf8);
Log.d("logr=", "title = " + doc.title());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("log2=", error.toString());
//requestQueue.stop();
}
});
// Add the request to the RequestQueue.
requestQueue.add(stringRequest);
requestQueue.start();
Output of the above code:
D/logr=: title = תיאטרון הפרינג' באר שבע
try 'https' instead of 'http' in your url

Categories

Resources