我正在尝试向我的服务器发送HTTP POST请求。 我的服务器接受这样的卷曲:
curl -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d "{\"City\":\"Lodz\", \"FirstName\":\"Patryk\"}" http://127.0.0.1:8000/addemployees
我也试图能够使用Android发送HTTP POST请求。 我的代码如下所示:
AsyncHttpClient httpClient = new AsyncHttpClient();
httpClient.addHeader("Accept", "application/json");
RequestParams jsonParams = new RequestParams();
jsonParams.put("City", city);
jsonParams.put("FirstName", city);
Log.i("requestParams: ", jsonParams.toString());
httpClient.post("http://myComputerIP:8000/addemployees", jsonParams, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject obj) {
String jsonResponse = obj.toString();
Log.i("TAG", "onSuccess: " + jsonResponse);
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
Log.e("TAG", "onFailure: " + errorResponse);
}
但是,我的Android代码出了问题,因为我收到了这个错误:
TAG: onFailure: {"message":"Internal Server Error"}
我尝试了几种解决方案,但没有一种方法有效。我该怎么改变它?感谢
答案 0 :(得分:0)
您应该以JSON格式发送数据。为此,您需要将包含已格式化的JSON字符串而不是POST
的{{1}}对象传递给StringEntity
方法。
RequestParams