Guest User

Untitled

a guest
Mar 20th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. // Triggered when user clicks button
  2. public void btn_click(View v) throws IOException {
  3. //create and send new http request
  4. Request req = new Request();
  5. req.url = "http://localhost/wijndisplay";
  6. System.out.println(req.send(null));
  7. }
  8.  
  9. public class Request {
  10. public String url;
  11.  
  12. private HttpClient client;
  13. private HttpGet get;
  14. private HttpResponse response;
  15.  
  16. public String send(Map<String, String> data) throws IOException {
  17. StringBuilder getStr = new StringBuilder("?");
  18.  
  19. if(data != null) {
  20. for (Map.Entry<String, String> entry : data.entrySet()) {
  21. getStr.append(entry.getKey())
  22. .append("=")
  23. .append(entry.getValue())
  24. .append("&");
  25. }
  26. url += getStr.toString();
  27. }
  28.  
  29. client = new DefaultHttpClient();
  30. get = new HttpGet(url);
  31. response = client.execute(get);
  32.  
  33. if(response.getStatusLine().getStatusCode() != 200) {
  34. throw new IOException("Failed to get server response");
  35. }
  36.  
  37. return EntityUtils.toString(response.getEntity());
  38. }
  39. }
Add Comment
Please, Sign In to add comment