Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. public static Object GetObject(int Method, int Type, String Params) {
  2. try {
  3. // Initialize HttpClient For Post Request
  4. HttpClient client = new DefaultHttpClient();
  5.  
  6. // Declare Response
  7. HttpResponse httpResponse = null;
  8.  
  9. switch (Method) {
  10. case HttpMethod.POST:
  11. // Create POST Request To The Given URL
  12. HttpPost post = new HttpPost(ServiceUrl);
  13.  
  14. // Set HttpPost Entity
  15. post.setEntity(new StringEntity(Params, HTTP.UTF_8));
  16.  
  17. // Add headers
  18. post.addHeader("Accept", "application/json");
  19. post.addHeader("Content-type", "application/json");
  20.  
  21. // Execute POST Request To The Given URL
  22. httpResponse = client.execute(post);
  23. break;
  24.  
  25. case HttpMethod.GET:
  26. // Create POST Request To The Given URL
  27. HttpGet get = new HttpGet(ServiceUrl + Params);
  28.  
  29. // Execute POST Request To The Given URL
  30. httpResponse = client.execute(get);
  31.  
  32. break;
  33. }
  34.  
  35. // Convert Response Entity To JsonString
  36. String responseJsonString = EntityUtils.toString(httpResponse
  37. .getEntity());
  38.  
  39. switch (Type) {
  40. case JSONType.Array:
  41. return new JSONArray(responseJsonString);
  42.  
  43. case JSONType.Object:
  44. return new JSONObject(responseJsonString);
  45. }
  46.  
  47. return null;
  48.  
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. return null;
  52. }
  53. }
  54.  
  55. public interface HttpMethod {
  56. int GET = 1;
  57. int POST = 2;
  58. }
  59. public interface JSONType {
  60. int Array = 1;
  61. int Object = 2;
  62. }
  63.  
  64. public static JSONObject Login(String username, String password) {
  65. return (JSONObject) GetObject(HttpMethod.GET, JSONType.Object, ServiceUrl + "auth/"
  66. + username + "/" + password);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement