POST请求未到达服务器 [英] POST request does not reach the server

查看:69
本文介绍了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("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";  
}  

}

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

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请求未到达服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆