POST 请求未到达服务器

发布于 2024-12-10 00:56:03 字数 1270 浏览 0 评论 0原文

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

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

客户端:

public void connect2server(View v) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://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";  
}  

}

这是如何引起的以及如何解决这个问题?

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:

Client:

public void connect2server(View v) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://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
    }
}

Server side:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤星 2024-12-17 00:56:03

没有错误,因为您捕获了错误但没有对它们执行任何操作。

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

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

,其中“com.name.pkg”只是一个标签,以便帮助您进行过滤。通常是程序的名称。

或者,非常常用的是使用 Toast.makeText(...).show(); 发送祝酒词。使用 e.getMessage() 但我不喜欢这样做,所以我不推荐它。您最好将一个字符串传递回调用函数并允许其进行祝酒。

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

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);

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文