我有一个web应用程序+数据库托管在python,我已经测试和工作的创建,登录和注销用户。现在我试图使请求在我的android应用程序写在java。然而,我一直得到308错误。
这是我的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);
,这是GsonRequest最重要的部分,它应该把JSON解析成字符串:
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;
}
错误:
E/Volley: [16210] NetworkUtility.shouldRetryException: Unexpected response code 308
for https://tddd80-app-joneri.azurewebsites.net/user
任何帮助都是感激的!
HTTP 308在这个意义上不是错误,它表示永久重定向。
很可能当你手动测试你的web服务器时,你使用了一个自动遵循HTTP重定向的工具。对于Volley,它看起来应该遵循重定向,除非协议在HTTP和HTTPS之间更改,参见此GitHub问题。
既然你正在使用的URL已经有https://
也许你的web服务器重定向到http://
URL?在这种情况下,你应该调整你的web服务器,因为出于安全原因,应该避免在未加密的HTTP上发布凭据。
或者您也可以检查HttpURLConnection.getFollowRedirects()
的值,可能由于某种原因是false
。