通过Android Studio和截击请求卷曲
我正在尝试使用RESTAPI,我需要提交一个如下所示的CURL请求通过Android Studio和截击请求卷曲,android,rest,api,http,android-volley,Android,Rest,Api,Http,Android Volley,我正在尝试使用RESTAPI,我需要提交一个如下所示的CURL请求 curl -H "Content-Type: application/json" -d '{"Name":"Noah Pasquale","JobTitle":"Owner","Phone":"089-0877311"}' -D- https://osacademy.outsystemscloud.com/ContactsAPI/rest/Contacts/CreateContact 我目前正在Android Studio上使
curl -H "Content-Type: application/json" -d '{"Name":"Noah Pasquale","JobTitle":"Owner","Phone":"089-0877311"}' -D- https://osacademy.outsystemscloud.com/ContactsAPI/rest/Contacts/CreateContact
我目前正在Android Studio上使用Volley,在过去的项目中,我已经能够生成HTTP POST并成功获取请求。然而,当我尝试与RESTAPI通信时,我的整个应用程序都崩溃了。这是我的密码
// Instantiate the RequestQueue.
final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
String server_urlpost = "https://osacademy.outsystemscloud.com/ContactsAPI/rest/Contacts/CreateContact"; //Points to target which is obtained from IPV4 Address from IP Config
StringRequest stringRequestpost = new StringRequest(Request.Method.POST, server_urlpost,
new Response.Listener<String>() {
@Override
public void onResponse(String response) { //Server Response Handler
PostResponse.setText(response);
requestQueue.stop();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { //On Error Response Handler
PostResponse.setText("Something went wrong...");
error.printStackTrace();
requestQueue.stop();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Name","Noah Pasquale");
params.put("JobTitle", "Owner");
params.put("Phone", "089-0877311");
Log.i(TAG, params.toString());
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers=new HashMap<String,String>();
headers.put("Content-Type", "application/json");
return headers;
}
};
//Starts Request
requestQueue.add(stringRequestpost);
我无法理解我做错了什么
Jerome L我知道我错在哪里了 我需要改变
headers.put(“内容类型”、“应用程序/json”)代码>
到headers.put(“接受”、“应用程序/json”)代码>
和使用
@Override
public byte[] getBody() throws AuthFailureError {}
而不是
@Override
protected Map<String, String> getParams() throws AuthFailureError {}
从以下来源获得答案:
显示堆栈跟踪我已添加我的堆栈跟踪。我期望的是响应代码200,但我得到的是400。您的PostResponse
textview未初始化,当您的响应到达时为空。哇,谢谢。我犯了一个新手错误。然而,你知道我的截击动作是否正确吗?因为我仍然无法与RESTAPI通信并收到正确的响应。非常感谢。
@Override
protected Map<String, String> getParams() throws AuthFailureError {}
@Override
public String getBodyContentType() { //Sets type to json
return "application/json";
}