신속하게 처리 한 게시물 요청을 Tomcat 서블릿에 보냅니다. 안드로이드에서 HttpPost 요청을하면 서블릿에서 요청을 볼 수 있지만 doGet 메서드가 호출 되었기 때문입니다. 왜 이런 일이 일어나고 doPost 메서드를 호출하도록 수정했는지 설명 할 수 있습니까?POST 요청이 doGet 메소드를 호출 중입니다.
@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();
}
}
및 요청 수신 서블릿 : 여기
는 POST 요청하고 서비스 방법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());
}
}
출력 : 요청이 수신
가져 오기!
헤더 : 호스트
: 헤더에 연결
헤더 : 사용자 에이전트
방법 요청 : 당신과의 HTTP POST를 수행하지 않는
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]
당신이 실행중인 생각하는 코드를 실행하는 것을 확실 중 하나의 예? – BalusC
동일한 시나리오를 실행했습니다. 당신이 그것을 알아낼 수 있었습니까? – Volti
내 문제를 파악했습니다. 내 서블릿에 대한 URL에서 마지막 슬래시를 생략했습니다. 여기에는 http : // url/to/servlet/here 대신 http : // url/to/servlet/here /가 있습니다. – Volti