Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.*;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;
import android.util.Log;

public class Main {
    public static String HttpGet(String url) {
        HttpClient client = new DefaultHttpClient();
        StringBuilder builder = new StringBuilder();
        HttpGet myget = new HttpGet(url);
        try {
            HttpResponse response = client.execute(myget);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            for (String s = reader.readLine(); s != null; s = reader.readLine()) {
                builder.append(s);
            }

            char cr = 65279;
            String t = String.valueOf(cr);
            String resultString = builder.toString();
            resultString = resultString.replace("\t", "").replace(t, "");

            return resultString;

        } catch (Exception e) {
            Log.v("url response", "false");
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T HttpGet(String url, Class<T> classOfT) {
        String jsonString = HttpGet(url);
        if (jsonString == null)
            return null;
        char cr = 65279;
        String t = String.valueOf(cr);
        jsonString = jsonString.replace("\t", "").replace(t, "");

        Gson g = new Gson();
        T result = null;
        try {
            result = g.fromJson(jsonString, classOfT);
            return result;
        } catch (Exception ex) {
            result = null;
        }
        return result;
    }
}