ESP32 Android App communication over HTTP - android

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.

Related

android volley basic request wont work for Special WebSite

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?

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

Volley throw 502 bad gateway

I'm using Volley to send Get method.Sometimes I have 502 bad gateway server error.I don't know what is a wrong or how i can solve my problem
This is a source code
public void polygonRequest(final String userID, final String secretKey) {
showpDialog("loading");
polygonModelList.clear();
String url = ServerUtil.polygon+userID+"/"+secretKey;
final JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
SavedPolygonJson polygonModel=new SavedPolygonJson();
polygonModel.setPolygonJson(response.toString());
polygonModel.saveInStorage();
parsePolygonRequest(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hidepDialog();
showOneItemDialog(error.toString(),false);
}
});
jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
CoreApplication.getInstance().getRequestQueue().add(jsObjRequest);
}
When I send my request with browser i have not any errors.How i can fixed this error...
How i can combine browser's request header with Volley request header?
thanks
Increase the the read time out session if you are using the retrofit.
Or in case of volley in crease the request time session from its default
for cross check hit the url directly in web browser and calculate the time that how much time it takes to fetch the response according to that you set the request session time
This happens because if your network lib does not get the response on predefined request time it show bad gateway 502

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

Android Volley JsonObjectRequest returns same response every time on mobile data

I am using Volley JsonObjectRequest to get data from server.
code snippet:
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
System.out.println("Response: " + response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
But I am getting same JSONObject response every time on mobile data connection.
Note: It's work perfectly on WiFi connection.
Is anyone facing this issue ? any solution ?
#BNK request.setShouldCache(false); worked for me. It's issue of volley cache management.
I assume that, when a request is sent:
It would hit the cache first and send that to onResponse
then when the results come through from the remote server it would provide it to the onResponse
If you use any of the default Request classes implemented in volley(e.g. StringRequest, JsonRequest, etc.), then call setShouldCache(false) right before adding the request object to the volley RequestQueue
request.setShouldCache(false);
myQueue.add(request);
You can also set expiration policy for cache.
See this answer for more details

Categories

Resources