Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. package th.ac.ictsu.helloadapternoon;
  2.  
  3. import android.os.StrictMode;
  4. import android.util.Log;
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.net.HttpURLConnection;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.util.HashMap;
  14.  
  15. public class ServerConnecter {
  16. public static String http = "http://";
  17. public static String ip = "172.16.30.13/";
  18. public static String dir = "016/";
  19.  
  20. public ServerConnecter()
  21. {
  22. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  23. StrictMode.setThreadPolicy(policy);
  24. }
  25. public String connect (String page,boolean post,boolean response,EntityModel model)
  26. {
  27. int responsecode = 0;
  28. StringBuilder result = new StringBuilder();
  29. String url = http+ip+dir+page;
  30. HttpURLConnection connection = null;
  31. try {
  32. URL urlcon = new URL(url);
  33. connection = (HttpURLConnection) urlcon.openConnection();
  34. connection.setDoInput(true);
  35. if (post == true)
  36. {
  37. connection.setDoOutput(true);
  38. connection.setRequestMethod("POST");
  39. connection.setConnectTimeout(5000);
  40. connection.setRequestProperty("Charset", "UTF-8");
  41. DataOutputStream outstream = new DataOutputStream(connection.getOutputStream());
  42. outstream.write(model.getDataEncode());
  43. }
  44.  
  45. if (response == true)
  46.  
  47. {
  48. String line;
  49. BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
  50. while ((line = br.readLine()) != null)
  51.  
  52. {
  53. result.append(line);
  54.  
  55. }
  56.  
  57.  
  58.  
  59. }
  60.  
  61. responsecode = connection.getResponseCode();
  62. Log.i("HTTP_connect", "Response Code : " + responsecode);
  63. } catch (MalformedURLException e) {
  64. Log.i("HTTP_connect","URL Error");
  65. e.printStackTrace();
  66. } catch (IOException e) {
  67. Log.i("HTTP_connect","Connection Error");
  68. e.printStackTrace();
  69. }
  70. connection.disconnect();
  71.  
  72. return result.toString();
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement