POST请求未到达服务器

编程入门 行业动态 更新时间:2024-10-28 14:25:40
本文介绍了POST请求未到达服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试通过POST请求将Android智能手机中的数据发送到REST Web服务.

I'm trying to send data from an Android smartphone via a POST request to a REST web service.

在客户端没有错误,但是在服务器端,如果我发送了数据,则不会执行任何代码:

There is no error on client side, but on the server side is no code executed if I have sent the data:

客户:

public void connect2server(View v) throws IOException { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("192.168.1.108:8182"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("id", "12345")); nameValuePairs.add(new BasicNameValuePair("stringdata", "data")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } }

服务器端:

public class data_server extends ServerResource { public static void main(String[] args) throws Exception { // Create the HTTP server and listen on port 8182 new Server(Protocol.HTTP, 8182, data_server.class).start(); } @Post public String toString1() throws IOException { System.out.println(getRequestEntity().getText()); return "ok"; } }

这是怎么引起的,我该如何解决?

How is this caused and how can I solve this?

推荐答案

没有错误,因为您正在捕获错误并且不对其进行任何操作.

There's no error because you are catching the errors and doing nothing with them.

尝试将以下内容添加到catch块中,您应该看到LogCat输出中发生了什么.记住要导入Log类

Try adding the following into the catch blocks and you should see what's happening in your LogCat output. Remember to the import the Log class

Log.w("com.name.pkg", e);

其中"com.name.pkg"只是一个标记,因此可以帮助您进行过滤.通常是程序的名称.

where "com.name.pkg" is just a tag so help you filter on. usually the name of the program.

或者,通常使用的是用Toast.makeText(...).show();发送烤面包.使用e.getMessage(),但是我不喜欢这样做,所以我不建议这样做.您最好将字符串传递回调用函数,并允许该函数进行敬酒.

Alternatively, quite commonly in use is to send a toast with Toast.makeText(...).show(); with e.getMessage() but I don't like doing that, so I don't recommend it. You would be better to pass a string back to the calling function and allow that to do the toast.

更多推荐

POST请求未到达服务器

本文发布于:2023-10-09 16:37:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!