Login/register 308 error

I have a web app + database hosted in python which I have tested and works for creating, logging in and logging out user. Now I'm trying to make the requests in my android application written in java. However, I keep getting 308 error. Here is my RegisterActivity.java
private void register() {
// Get headers, method and url to be used in the request
final String user = username.getText().toString();
final String pass = password.getText().toString();
Map<String, String> userObject = new HashMap<>();
userObject.put("username", user);
userObject.put("password", pass);
final int method = Request.Method.POST;
final String url = "https://tddd80-app-joneri.azurewebsites.net/user";

// Create request
GsonRequest request = new GsonRequest(Request.Method.POST, url, userObject,
response -> System.out.println("Success! Response: " + response),
error -> System.out.println("Error with request: " + error.getMessage());

// Add the request to the Volley request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
private void register() {
// Get headers, method and url to be used in the request
final String user = username.getText().toString();
final String pass = password.getText().toString();
Map<String, String> userObject = new HashMap<>();
userObject.put("username", user);
userObject.put("password", pass);
final int method = Request.Method.POST;
final String url = "https://tddd80-app-joneri.azurewebsites.net/user";

// Create request
GsonRequest request = new GsonRequest(Request.Method.POST, url, userObject,
response -> System.out.println("Success! Response: " + response),
error -> System.out.println("Error with request: " + error.getMessage());

// Add the request to the Volley request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
and this is the most important part of GsonRequest which should parse JSON into string:
public class GsonRequest extends Request<String> {
private final Map<String, String> headers;
private final Response.Listener<String> listener;

public GsonRequest(int method, String url, Map<String, String> headers,
Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.headers = headers;
this.listener = listener;
}
public class GsonRequest extends Request<String> {
private final Map<String, String> headers;
private final Response.Listener<String> listener;

public GsonRequest(int method, String url, Map<String, String> headers,
Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.headers = headers;
this.listener = listener;
}
Any help is appreciated!
27 Replies
JavaBot
JavaBot3y ago
This post has been reserved for your question.
Hey @Erky! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
daysling
daysling3y ago
@Erky Please attach the error as well.
Erky
ErkyOP3y ago
D/Volley: [16199] NetworkUtility.logSlowRequests: HTTP response for request=<[ ] https://tddd80-app-joneri.azurewebsites.net/user 0xce2c15ac NORMAL 1> [lifetime=5143], [size=283], [rc=308], [retryCount=1]
E/Volley: [16199] NetworkUtility.shouldRetryException: Unexpected response code 308 for https://tddd80-app-joneri.azurewebsites.net/user
D/Volley: [16199] NetworkUtility.logSlowRequests: HTTP response for request=<[ ] https://tddd80-app-joneri.azurewebsites.net/user 0xce2c15ac NORMAL 1> [lifetime=5143], [size=283], [rc=308], [retryCount=1]
E/Volley: [16199] NetworkUtility.shouldRetryException: Unexpected response code 308 for https://tddd80-app-joneri.azurewebsites.net/user
daysling
daysling3y ago
The website is returning 308 response code, was it configured to do that?
Erky
ErkyOP3y ago
dont think so # Create user @app.route('/user/', methods=['POST']) def create_user(): # Get the data from the database data = request.get_json() username = data['username'] password = data['password'] # Handle already existing user user_already_exists = User.query.filter_by(username=username).first() if user_already_exists is not None: return jsonify(response='Error: User already exists'),409 # Generate password hash and add it to a new user new_user = User(username=username) new_user.generate_password_hash(password) # Add the new user to the database db.session.add(new_user) db.session.commit() # Return value return jsonify(response='Successfully created a new user'), 200
daysling
daysling3y ago
308 is a redirect code..
Erky
ErkyOP3y ago
yeah, maybe i can set it to follow redirects?
daysling
daysling3y ago
Yes Try that
Erky
ErkyOP3y ago
how
daysling
daysling3y ago
How are you sending request? Through which library/package? HttpClient? HttpRequest?
Erky
ErkyOP3y ago
in python im using flask to send requests i think, and then im using volley to post requests, and then gson library to convert but not sure, something in volley i think, sorry not 100% sure
daysling
daysling3y ago
@Override
public void deliverError(final VolleyError error) {
Log.d(TAG, "deliverError");

final int status = error.networkResponse.statusCode;
// Handle 30x
if(HttpURLConnection.HTTP_MOVED_PERM == status || status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_SEE_OTHER) {
final String location = error.networkResponse.headers.get("Location");
Log.d(TAG, "Location: " + location);
final GsonRequest<T> request = new GsonRequest<T>(method, location, jsonRequest, this.requestContentType, this.clazz, this.ttl, this.listener, this.errorListener);
// Construct a request clone and change the url to redirect location.
RequestManager.getRequestQueue().add(request);
}
}
@Override
public void deliverError(final VolleyError error) {
Log.d(TAG, "deliverError");

final int status = error.networkResponse.statusCode;
// Handle 30x
if(HttpURLConnection.HTTP_MOVED_PERM == status || status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_SEE_OTHER) {
final String location = error.networkResponse.headers.get("Location");
Log.d(TAG, "Location: " + location);
final GsonRequest<T> request = new GsonRequest<T>(method, location, jsonRequest, this.requestContentType, this.clazz, this.ttl, this.listener, this.errorListener);
// Construct a request clone and change the url to redirect location.
RequestManager.getRequestQueue().add(request);
}
}
Something like this Implement this in your GsonRequest.. Otherwise.. request.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f)) Could work Oh I think I know why this is happening @Erky Change your url from https://tddd80-app-joneri.azurewebsites.net/user to https://tddd80-app-joneri.azurewebsites.net/user/ Or from https://tddd80-app-joneri.azurewebsites.net/user/ to https://tddd80-app-joneri.azurewebsites.net/user(PYTHON)
Erky
ErkyOP3y ago
i dont see what changed?
daysling
daysling3y ago
The / in the last
Erky
ErkyOP3y ago
E/Volley: [16230] NetworkUtility.shouldRetryException: Unexpected response code 400 for https://tddd80-app-joneri.azurewebsites.net/user/ now got this error
daysling
daysling3y ago
Check your python app Did it log anything Well It's clear why that happened
Erky
ErkyOP3y ago
unfortunately dont think i can do that, i think my friend is hosting the web app why's that?
daysling
daysling3y ago
data = request.get_json() // Tries to load from body
username = data['username']
password = data['password']
data = request.get_json() // Tries to load from body
username = data['username']
password = data['password']
You have data set at headers Not body.. Use request.headers to access them.
Erky
ErkyOP3y ago
ohh I see at userObject or? not seeing headers
Map<String, String> userObject = new HashMap<>();
userObject.put("username", user);
userObject.put("password", pass);
final String url = "https://tddd80-app-joneri.azurewebsites.net/user/";

// Create request
GsonRequest request = new GsonRequest(Request.Method.POST, url, userObject,
Map<String, String> userObject = new HashMap<>();
userObject.put("username", user);
userObject.put("password", pass);
final String url = "https://tddd80-app-joneri.azurewebsites.net/user/";

// Create request
GsonRequest request = new GsonRequest(Request.Method.POST, url, userObject,
because its still going into error -> im going here response -> not here
daysling
daysling3y ago
public GsonRequest(int method, String url, Map<String, String> headers,
Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.headers = headers;
this.listener = listener;
}
public GsonRequest(int method, String url, Map<String, String> headers,
Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.headers = headers;
this.listener = listener;
}
userObject is headers
Erky
ErkyOP3y ago
yes , but userObject is a Map with <string> <string> which should take in username and password, but i dont get how i can modify so i get the "headers" i have the strings user pass which i want to pass as headers into my gsonrequest
// Get headers, method and url to be used in the request
final String user = username.getText().toString();
final String pass = password.getText().toString();
Map<String, String> userObject = new HashMap<>();
userObject.put("username", user);
userObject.put("password", pass);
final String url = "https://tddd80-app-joneri.azurewebsites.net/user/";

// Create request
GsonRequest request = new GsonRequest(Request.Method.POST, url, userObject,
response -> System.out.println("Success! Response: " + response),
error -> System.out.println("Error: " + error.getMessage()));

// Add the request to the Volley request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
// Get headers, method and url to be used in the request
final String user = username.getText().toString();
final String pass = password.getText().toString();
Map<String, String> userObject = new HashMap<>();
userObject.put("username", user);
userObject.put("password", pass);
final String url = "https://tddd80-app-joneri.azurewebsites.net/user/";

// Create request
GsonRequest request = new GsonRequest(Request.Method.POST, url, userObject,
response -> System.out.println("Success! Response: " + response),
error -> System.out.println("Error: " + error.getMessage()));

// Add the request to the Volley request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
daysling
daysling3y ago
You'll need to either post JSON Body data or accept data form the headers instead of body in python flask
Erky
ErkyOP3y ago
how can i post json body data?
daysling
daysling3y ago
GeeksforGeeks
How to Post Data to API using Volley in Android? - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Erky
ErkyOP3y ago
is it possible to post the user and pass strings as parameters in my request? so I should make a StringRequest instead? tried what you suggested and now got 500 error,
// Get headers and url to be used in the request
final String user = username.getText().toString();
final String pass = password.getText().toString();
Map<String, String> userObject = new HashMap<>();
userObject.put("username", user);
userObject.put("password", pass);
final String url = "https://tddd80-app-joneri.azurewebsites.net/user/";

StringRequest request = new StringRequest(Request.Method.POST, url,
response -> {
System.out.println("some success!!!");
},
(Response.ErrorListener) error -> {
System.out.println("some error!!!");
}) {
@Override
public byte[] getBody() {
JSONObject json = new JSONObject();
try {
json.put("username", user);
json.put("password", pass);
} catch (JSONException e) {
e.printStackTrace();
}
return json.toString().getBytes();
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type","application/json");
return headers;
}
@Override
public String getBodyContentType() {
return "application/json";
}
};

// Add the request to the Volley request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);


E/Volley: [16370] NetworkUtility.shouldRetryException: Unexpected response code 500 for https://tddd80-app-joneri.azurewebsites.net/user/
I/System.out: some error!!!
// Get headers and url to be used in the request
final String user = username.getText().toString();
final String pass = password.getText().toString();
Map<String, String> userObject = new HashMap<>();
userObject.put("username", user);
userObject.put("password", pass);
final String url = "https://tddd80-app-joneri.azurewebsites.net/user/";

StringRequest request = new StringRequest(Request.Method.POST, url,
response -> {
System.out.println("some success!!!");
},
(Response.ErrorListener) error -> {
System.out.println("some error!!!");
}) {
@Override
public byte[] getBody() {
JSONObject json = new JSONObject();
try {
json.put("username", user);
json.put("password", pass);
} catch (JSONException e) {
e.printStackTrace();
}
return json.toString().getBytes();
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type","application/json");
return headers;
}
@Override
public String getBodyContentType() {
return "application/json";
}
};

// Add the request to the Volley request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);


E/Volley: [16370] NetworkUtility.shouldRetryException: Unexpected response code 500 for https://tddd80-app-joneri.azurewebsites.net/user/
I/System.out: some error!!!
oh wait i think i got it to work! thank you man 🙂
JavaBot
JavaBot3y ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot3y ago
Post Closed
This post has been closed by <@272473391041347585>.

Did you find this page helpful?