POST 请求未到达服务器
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有错误,因为您捕获了错误但没有对它们执行任何操作。
尝试将以下内容添加到 catch 块中,您应该会看到 LogCat 输出中发生了什么。请记住导入 Log 类
,其中“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
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.