gpt4 book ai didi

android - BasicNetwork.performRequest - 意外响应代码 400 (POST)

转载 作者:搜寻专家 更新时间:2023-11-01 09:36:25 24 4
gpt4 key购买 nike

当我尝试使用 Volley StringRequest 或 JsonObjectRequest 通过 rest api 获取数据时。我总是收到 400 错误。它与 postman 合作良好。 http请求方式为POST,Content-Type为application/x-www-form-urlencoded。我还检查了我的参数拼写,我觉得没问题。我尝试了 StringRequest 和 JsonObjectRequest,结果是 E/Volley: [10283] BasicNetwork.performRequest: Unexpected response code 400。下面是我的代码。我附上了截图。

enter image description here

这是新的截图 Error in code

StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
viewHelper.hideDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
viewHelper.hideDialog();
}
}){
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", email);
params.put("password", password);
return params;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}

@Override
public String getBodyContentType(){
return "application/x-www-form-urlencoded";
}
};

stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

// Adding request to request queue
AppController.getInstance().addToRequestQueue(stringRequest, "Authenticating User");

我也尝试过 volley JSONObjectRequest

            Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", email);
params.put("password", password);

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, AppConfig.URL_LOGIN, new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
};

谁能帮我解决这个问题。

最佳答案

下面是使用 Volley 库的 POST 请求的工作示例。
将此添加到您的 build.gradle (Module:app) -编译'com.mcxiaoke.volley:library:1.0.19'

StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
progressDialog.dismiss();

JSONObject jsonObject = null;

try {
jsonObject = new JSONObject(response);

if(jsonObject.has("success")){
if(jsonObject.getBoolean("success") == true){
JSONObject userObject = new JSONObject(jsonObject.getString("user"));

Map<String, String> loginDetails = new HashMap<String, String>();
loginDetails.put(KEY_IS_LOGGED_IN, "true");
loginDetails.put(ACCESS_TOKEN, jsonObject.getString(ACCESS_TOKEN));
loginDetails.put(USERID, userObject.getString(USERID));
loginDetails.put(FIRSTNAME, userObject.getString(FIRSTNAME));
loginDetails.put(LASTNAME, userObject.getString(LASTNAME));
loginDetails.put(EMAIL, userObject.getString(EMAIL));

session = new SessionManager(_myActivity);
session.setLogin(loginDetails);

Intent intent = new Intent(LoginActivity.this, SearchActivity.class);
intent.putExtra("FROM_ACTIVITY", "LoginActivity");
startActivity(intent);
LoginActivity.this.finish();
}else{
btnLogin.setClickable(true);
btnLogin.setEnabled(true);
Toast.makeText(getApplicationContext(), "Username or Password does not matched!", Toast.LENGTH_LONG).show();
}
}

} catch (JSONException e) {
btnLogin.setClickable(true);
btnLogin.setEnabled(true);
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Something went wrong, Please try agian", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
progressDialog.dismiss();

btnLogin.setClickable(true);
btnLogin.setEnabled(true);
}
}){
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", email);
params.put("password", password);
return params;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}

@Override
public String getBodyContentType(){
return "application/x-www-form-urlencoded";
}
};

stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

// Adding request to request queue
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());

mRequestQueue.add(stringRequest);

如果有人遇到上述代码的问题,请发表评论。
感谢所有试图帮助我的人。

关于android - BasicNetwork.performRequest - 意外响应代码 400 (POST),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42827279/

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