0

我正在尝试在我的客户端应用程序中执行 PUT/POST 以更新我的数据库。我正在使用 HttpClient。

以下哪种方法更正确,为什么它们都不起作用?

第一个:HTTP/1.1 415 不支持的媒体类型

   List <NameValuePair> nvps = new ArrayList <NameValuePair>();

   nvps.add(new BasicNameValuePair("Accept", "application/json"));
   nvps.add(new BasicNameValuePair("Content-Type", "application/json"));

   nvps.add(new BasicNameValuePair("userID", "user5"));
   nvps.add(new BasicNameValuePair("FirstName", "teste"));
   nvps.add(new BasicNameValuePair("LastName", "2"));

   httpPost.setEntity(new UrlEncodedFormEntity(nvps));

第二:HTTP/1.1 500 内部服务器错误

    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");

    JSONObject obj = new JSONObject();  
    obj.put("userID", "user5");
    obj.put("FirstName", "teste");
    obj.put("LastName", "2");

    httpPost.setEntity(new StringEntity(obj.toString(), "UTF-8"));

最后我这样做:HttpResponse response = httpclient.execute(httpPost);

或者问题可能出在 URL ... 我有:HttpPost httpPost= new HttpPost("http://localhost:8080/LULServices/webresources/entities.user"); 我想在我的用户服务中添加一个新用户(来自 de 数据库的用户表)

谢谢。

4

1 回答 1

0

我已经解决了第二种方法的问题,只需将第一个大写字母更改为小写即可。真是愚蠢的错误..它被定义为'firstName',我写它就像写在DataBase表中一样。

于 2013-03-15T11:09:05.850 回答