gpt4 book ai didi

java - POST 请求正在调用 doGet 方法

转载 作者:太空狗 更新时间:2023-10-29 14:28:31 26 4
gpt4 key购买 nike

我正在将 Post 请求发送到我快速搜索过的 Tomcat Servlet。当我在 Android 上发出 HttpPost 请求时,我发现我在 servlet 中看到了请求,但那是因为调用了 doGet 方法。任何人都可以向我解释为什么会发生这种情况以及如何修复它以调用 doPost 方法吗?

这里是发出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 request received!!!

header: host

header: connection

header: user-agent

Method request: 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

最佳答案

您没有使用您的代码执行 http POST,因为您的实体是空的。

Here's在 Android 中执行 http POST 的方法之一的示例

关于java - POST 请求正在调用 doGet 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9355284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com