POST请求未到达服务器

时间:2011-10-16 14:06:32

标签: android http rest post restlet

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

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

客户端:

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";  
}  

}

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

1 个答案:

答案 0 :(得分:1)

没有错误,因为你正在捕捉错误而对它们一无所知。

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

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

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

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

相关问题