gpt4 book ai didi

android - 截击错误 : Unexpected response code 400

转载 作者:行者123 更新时间:2023-11-29 20:01:59 25 4
gpt4 key购买 nike

我使用下面的代码来获取 Volley 请求并获取 json:

  JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.POST,
service_address , null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response: " + response.toString());
if (response != null) {
parseJsonFeed(response);
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
}){


@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("username", username);
params.put("password", password);

return params;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
// Removed this line if you dont need it or Use application/json
// params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};

但在运行后它在 logcat 中给我这个错误:

BasicNetwork.performRequest: Unexpected response code 400

我读了this link ,但这对我没有帮助。我很困惑我的代码出了什么问题?

最佳答案

我不确定这是否有任何帮助,但以下是我如何使用 Volley 进行登录请求:

/**
* Method that checks given user credentials with the ones in the online DB
* @param v the view that activated this method
*/
public void logIn(View v) {
final String email = String.valueOf(((EditText) findViewById(R.id.inputEmail)).getText());
final String password = String.valueOf(((EditText) findViewById(R.id.inputPassword)).getText());

String tag_string_req = "string_req";
final String TAG = AppController.class
.getSimpleName();
String url = "http://android.diggin.io/diggin/v1/login";
StringRequest strReq = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d(TAG, response);
try {
final JSONObject jsonObject = new JSONObject(response);
if (!jsonObject.getBoolean("error")) {
apiKey = jsonObject.getString("apiKey");
SharedPreferences sharedPref = getSharedPreferences(getString(R.string.apiKey), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(getString(R.string.apiKey), apiKey);
editor.commit();
refreshApiKey();
} else {
//Send message when something goes wrong
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(LoginActivity.this);
try {
dlgAlert.setMessage(jsonObject.getString("message"));
} catch (JSONException e) {
dlgAlert.setMessage("Something went wrong, please try again");
}
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
//Send message when something goes wrong
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(LoginActivity.this);
dlgAlert.setMessage("Error while logging in, please try again");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
});
}
}) {

@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("password", password);

return params;
}
};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

关于android - 截击错误 : Unexpected response code 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36135645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com