gpt4 book ai didi

android - 如何在带有参数和 header 的android Volley库中使用POST请求?

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:49 26 4
gpt4 key购买 nike

我正在尝试学习用于将数据发布到网络服务中的 Volley 库。我需要实现用户注册表单,下面是带有参数和标题的 postman 图像... postman screen for post request

现在的问题是,我遇到了以下错误

com.android.volley.ServerError

这是我的 volley post 方法的代码。

public void postNewComment(){
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://myurl/api/users";
JSONObject jsonBody = new JSONObject();
jsonBody.put("email", "test1@gmail.com");
jsonBody.put("user_type", "C");
jsonBody.put("company_id", "0");
jsonBody.put("status", "A");
jsonBody.put("password", "123456");

final String requestBody = jsonBody.toString();

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Basic " + "My_auth_key");
headers.put("Content-Type", "application/json");

return headers;
}

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};

requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}

请指出我错在哪里。 URL 与 postman 一起工作正常,正如您所看到的,我需要设置 2 个标题。我还使用 AsyncTask 尝试了这个 Url post 方法,它运行良好。现在我需要使用 volley 库来实现它。请建议。谢谢你。

这是我的 logcat 错误:

E/Volley: [81910] BasicNetwork.performRequest: Unexpected response code 405 for "Myurl"

最佳答案

**试试这个**

    private void sendWorkPostRequest() {

try {
String URL = "";
JSONObject jsonBody = new JSONObject();

jsonBody.put("email", "abc@abc.com");
jsonBody.put("password", "");
jsonBody.put("user_type", "");
jsonBody.put("company_id", "");
jsonBody.put("status", "");

JsonObjectRequest jsonOblect = new JsonObjectRequest(Request.Method.POST, URL, jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {

Toast.makeText(getApplicationContext(), "Response: " + response.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

onBackPressed();

}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Basic " + "c2FnYXJAa2FydHBheS5jb206cnMwM2UxQUp5RnQzNkQ5NDBxbjNmUDgzNVE3STAyNzI=");//put your token here
return headers;
}
};
VolleyApplication.getInstance().addToRequestQueue(jsonOblect);

} catch (JSONException e) {
e.printStackTrace();
}
// Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();

}
}

关于android - 如何在带有参数和 header 的android Volley库中使用POST请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46171093/

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