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.1开发者_开发百科68.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?
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.
精彩评论