我正在向Tomcat Servlet发送Post请求,我很快就一起翻找了。当我在Android上发出HttpPost请求时,我看到我在servlet中看到了请求,但这是因为调用了doGet方法。任何人都可以向我解释为什么会发生这种情况以及我如何修复它以调用doPost方法?
以下是发布帖子请求的服务方法:
@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());
}
}
输出:
收到请求!!!
标题:主持人
标题:连接
header:user-agent
方法请求: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 java.io.ByteArrayInputStream@4058d760
I SendLocationIntentService: handling intent
I SendLocationIntentService: JSON object: {"long":0,"time":1329792743161,"lat":0}
I SendLocationIntentService: Method POST
I SendLocationIntentService: Entity java.io.ByteArrayInputStream@40594050