我有一个Android应用程序的问题,称为使用Jersey实现的Rest服务:android应用程序发送带有json参数的POST请求,我得到错误405:“方法不允许”。 这很奇怪,因为我在应用程序的另一部分使用相同的代码,一切运行正常。 我在网上搜索,我试图在请求中指定Post,但错误仍然存在。 我希望有人能告诉我我看不到的错误。 这是来自android应用程序的代码:
private class VoteContent extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... params) {
String email = params[0];
String content = params[1];
String value = params[2];
String comment=params[3];
String url = Constants.baseUrl + "/content/votecontent";
try {
String json;
JSONObject jsonObject = new JSONObject();
jsonObject.put("email", email);
jsonObject.put("content", content);
jsonObject.put("value", value);
jsonObject.put("comment", comment);
json = jsonObject.toString();
Log.d("JSON", url);
URL uri = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) uri.openConnection();
urlConnection.setDoOutput(true);
// urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8");
out.write(json);
out.close();
Log.d("SERVER", "" + urlConnection.getResponseCode());
Log.d("SERVER", "" + urlConnection.getResponseMessage());
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return null;
}
protected void onPostExecute(String aVoid){
super.onPostExecute(aVoid);
/*
TextView tv=(TextView)findViewById(R.id.textView1);
if (aVoid==null)
tv.setText("error");
else
tv.setText(aVoid);
*/
}
}
和POST:
@POST
@Path("/votecontent")
@Produces(MediaType.APPLICATION_JSON)
@Consumes({ MediaType.APPLICATION_JSON,MediaType.TEXT_PLAIN })
public Content voteContent(InputStream data) {
StringBuilder builder = new StringBuilder();
AppDaoImpl cd = AppDaoImpl.getInstance();
Content risultato=null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(data));
String line = null;
while ((line = in.readLine()) != null) {
builder.append(line);
}
System.out.println("Data Received: " + builder.toString());
JSONObject json = new JSONObject(builder.toString());
String email=json.getString("email");
String comment=json.getString("comment");
int content=Integer.parseInt(json.getString("content"));
double value=Double.parseDouble(json.getString("value"));
int id_user=cd.containsUser(email);
cd.voteContent(id_user,content,value,comment);
System.out.println("result OK");
} catch (Exception e) {
System.out.println("Error Parsing: - ");
}
return ris;
}here