Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Object GetObject(int Method, int Type, String Params) {
- try {
- // Initialize HttpClient For Post Request
- HttpClient client = new DefaultHttpClient();
- // Declare Response
- HttpResponse httpResponse = null;
- switch (Method) {
- case HttpMethod.POST:
- // Create POST Request To The Given URL
- HttpPost post = new HttpPost(ServiceUrl);
- // Set HttpPost Entity
- post.setEntity(new StringEntity(Params, HTTP.UTF_8));
- // Add headers
- post.addHeader("Accept", "application/json");
- post.addHeader("Content-type", "application/json");
- // Execute POST Request To The Given URL
- httpResponse = client.execute(post);
- break;
- case HttpMethod.GET:
- // Create POST Request To The Given URL
- HttpGet get = new HttpGet(ServiceUrl + Params);
- // Execute POST Request To The Given URL
- httpResponse = client.execute(get);
- break;
- }
- // Convert Response Entity To JsonString
- String responseJsonString = EntityUtils.toString(httpResponse
- .getEntity());
- switch (Type) {
- case JSONType.Array:
- return new JSONArray(responseJsonString);
- case JSONType.Object:
- return new JSONObject(responseJsonString);
- }
- return null;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- public interface HttpMethod {
- int GET = 1;
- int POST = 2;
- }
- public interface JSONType {
- int Array = 1;
- int Object = 2;
- }
- public static JSONObject Login(String username, String password) {
- return (JSONObject) GetObject(HttpMethod.GET, JSONType.Object, ServiceUrl + "auth/"
- + username + "/" + password);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement