POST请求正在调用doGet方法
我发送POST请求到一个Tomcat Servlet,我很快翻遍了一起。当我在Android上发出HttpPost请求时,我发现我看到了servlet中的请求,但这是因为调用了doGet方法。任何人都可以向我解释为什么会发生这种情况,以及我如何修复它以调用doPost方法?POST请求正在调用doGet方法
这里的服务方法使POST请求:
@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "handling intent");
// get Longitude and Latitude
Bundle bundle = intent.getExtras();
double longitude = bundle.getDouble("longitude");
double latitude = bundle.getDouble("latitude");
// int id = bundle.getInt("id");
long time = System.currentTimeMillis();
// log location in local db
// send location up to db repository (repositories)
JSONObject jObj = new JSONObject();
try {
jObj.put("long", longitude);
jObj.put("lat", latitude);
jObj.put("userId", 1);
jObj.put("time", time);
Log.i(TAG, "JSON object: " + jObj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
try {
StringEntity se = new StringEntity("JSON" + jObj.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
String url = [http://url/to/servlet/here];
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setEntity(se);
HttpResponse response;
response = httpClient.execute(post);
// check response
if (response != null) {
Log.i(TAG, "Message received");
}
} catch (Exception e) {
e.printStackTrace();
}
}
并且servlet接收请求:
public class ReceiveLocation extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Post request received");
response.setContentType("application/json");
PrintWriter out = response.getWriter();
try {
StringBuilder sb = new StringBuilder();
String s;
while ((s = request.getReader().readLine()) != null) {
sb.append(s);
}
System.out.println("sb: " + sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Get request received!!!");
}
}
输出是 “获取接收到的请求!”
编辑
我临时改变doGet方法给我一个跟踪上我认为可能是重要的几件事情(我是新来的这一切,请让我知道如果我贴对这种情况没有帮助)。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Get request received!!!");
Enumeration<String> enumerator = request.getHeaderNames();
while (enumerator.hasMoreElements()) {
System.out.println("header: " + enumerator.nextElement());
}
System.out.println("Method request: " + request.getMethod().toString());
Enumeration<String> attrEnumerator = request.getAttributeNames();
while (attrEnumerator.hasMoreElements()) {
System.out.println("attr:" + attrEnumerator.nextElement());
}
Enumeration<String> paramEnumerator = request.getParameterNames();
while (paramEnumerator.hasMoreElements()) {
System.out.println("param:" + paramEnumerator.nextElement());
}
}
输出:
获取接收到的请求!
头:主机
头:连接
头:用户代理
方法请求:GET
Output from Android:
I TreasureHuntActivity: Provider network has been selected.
I TreasureHuntActivity: Init Lat: 30280
I TreasureHuntActivity: Init Long: -97744
I SendLocationIntentService: handling intent
I SendLocationIntentService: JSON object: {"long":0,"time":1329792722150,"lat":0}
I SendLocationIntentService: Method POST
I SendLocationIntentService: Entity [email protected]
I SendLocationIntentService: handling intent
I SendLocationIntentService: JSON object: {"long":0,"time":1329792743161,"lat":0}
I SendLocationIntentService: Method POST
I SendLocationIntentService: Entity [email protected]
你不是做一个HTTP POST你代码,因为你的实体是空的。
Here's做一个HTTP POST在Android的方式
你在哪里看到实体是空的?无论是否为空,OP都使用“HttpPost”而不是“HttpGet”。 – BalusC 2012-02-20 02:42:24
你是否试图附加JSON文件/对象作为附件,并通过POST发送?或者你只是传递json文件/对象的内容?您是否尝试打印出您的servlet接收到的内容? – androidnoob 2012-02-20 03:06:41
我想通过json对象传递数据。我对servlet编程很陌生,但我会看到我能找到的有关发送的内容。 – user766495 2012-02-20 03:19:26
你确定你运行你认为你正在运行的代码的一个例子吗? – BalusC 2012-02-20 02:43:18
我遇到了这种情况。你能想出来吗? – Volti 2013-08-23 21:02:42
我想出了我的问题。我正在省略servlet的URL的最后一个正斜杠。 http:// url/to/servlet/here /而不是http:// url/to/servlet/here。 – Volti 2013-08-26 17:16:20