Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;

import java.net.URL;

import android.util.Log;

public class Main {
    public static String getServerResponse(String urlRequest) {

        Log.d("urlRequest", urlRequest);
        String response = "";
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(urlRequest).openConnection();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            response = read(conn.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.d("response", response);
        return response.trim();
    }

    private static String read(InputStream in) throws IOException {

        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);
        for (String line = r.readLine(); line != null; line = r.readLine()) {
            sb.append(line);
        }
        in.close();
        return sb.toString();
    }
}