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
Related
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.
I have been trying to upload my data from android to my server using java, volley and laravel but I am getting. BasicNetwork.performRequest: Unexpected response code 500. I have used postman to test my api and it is working perfectly well.
What could be the cause?
Java code
RequestQueue queue = Volley.newRequestQueue(this); // this = context
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
pd.dismiss();
Log.i("error", String.valueOf( response.toString()));
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error
Toast.makeText(SignUpActivity.this, "Network Error. Please Try Again.", Toast.LENGTH_LONG).show();;
}
}
) {
#Override
protected Map<String, String> getParams()
{
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("email", email);
parameters.put("password", password);
parameters.put("vCode", vCode);
parameters.put("phone",phone);
return parameters;
}
};
queue.add(postRequest);
Code 500 means that there's a problem at server side. If you're using laravel you should have a web server (as apache, nginx...) which generates error logs.
Check error logs and there you may find what the error is (maybe your app is sending data in a way that server doesn't understand, data is not being sent properly...).
Apache error log location in Linux
Nginx error log location
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
(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
Link to Rails server. https://afternoon-sea-5654.herokuapp.com.
I want to send a simple JSON POST to attempt to login. I get the following volley errors
Error
04-09 14:03:56.156 3002-3031/com.digitalnatives.volleytest E/Volley﹕ [244] BasicNetwork.performRequest: Unexpected response code 500 for https://afternoon-sea-5654.herokuapp.com/sessions/create
04-09 14:03:56.160 3002-3002/com.digitalnatives.volleytest E/Volley﹕ [1] 4.onErrorResponse: Error:
I can't tell if it's down to the way I am formatting the request. Here is an example login request using curl manually.
Login -
curl -X POST -d "user[email]=ywaghmare5203#gmail.com&user[password]=12345678&" https://afternoon-sea-5654.herokuapp.com/sessions/create.json
Request perameter: email, password,
Response Perameter:
{"id":10,"username":"yogeshwaghmare1","email":"ywaghmare5203#gmail.com","password_hash":"$2a$10$pvLhzJlVz8Hl86O7N/ekiO2wrwNxbfTZlYPtccY4f7vXYNFs1vq6a","password_salt":"$2a$10$pvLhzJlVz8Hl86O7N/ekiO","last_login_time":null,"is_active":null,"contact_number":"123456","created_at":"2015-04-01T19:20:37.552Z","updated_at":"2015-04-01T19:20:37.552Z"}
JSONObjectRequest code
public void loginTest() {
private final String LOGIN = "https://afternoon-sea-5654.herokuapp.com/sessions/create";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
// old test code : params.put("user[email]=test1#gmail.com&", "user[password]=12345678&");
params.put("user[email]", "test1#gmail.com");
params.put("user[password]", "12345678");
JsonObjectRequest req = new JsonObjectRequest(LOGIN, new JSONObject(params),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response:%n %s", response.toString(4));
responseText.setText("Worked!");
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
responseText.setText("Nope");
}
});
// add the request object to the queue to be executed
AppController.getInstance().addToRequestQueue(req);
}
Actually in response you are not Getting the JSON data. It is returning a HTML message regarding redirection. Your response is 302
Response code 500 means its a server sided syntax error. You can test your api through online API testing platform like Runscope. It really save our time and confusion when we collaborate with web team and android team.
Runscope Link