com.mehmetakiftutuncu.eshotroid.utilities.Request.java Source code

Java tutorial

Introduction

Here is the source code for com.mehmetakiftutuncu.eshotroid.utilities.Request.java

Source

/*
 * Copyright (C) 2015 Mehmet Akif Ttnc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mehmetakiftutuncu.eshotroid.utilities;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Request {
    public static String get(String url) {
        try {
            Log.info(Request.class, "Making GET request to url: " + url);

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);

            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                Log.error(Request.class,
                        "GET request failed, invalid status code! status code: " + statusCode + ", url: " + url);
                return null;
            } else {
                HttpEntity httpEntity = httpResponse.getEntity();
                String result = EntityUtils.toString(httpEntity, "UTF-8");

                Log.info(Request.class, "GET result from url: " + url);
                Log.info(Request.class, result);

                return result;
            }
        } catch (Exception e) {
            Log.error(Request.class, "GET request failed! url: " + url, e);
            return null;
        }
    }
}