HTTP send Get - Android Network

Android examples for Network:HTTP Get

Description

HTTP send Get

Demo Code


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

import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import android.util.Log;

public class Main {
    public static String sendGet(String url, String parmas) {
        String result = "";
        BufferedReader bufferedReader = null;
        String urlName = url + "?" + parmas;
        try {//from w ww.ja  va2  s. c o  m
            URL realUrl = new URL(urlName);
            try {
                URLConnection urlConnection = realUrl.openConnection();
                urlConnection.setRequestProperty("accept", "*/*");
                urlConnection
                        .setRequestProperty("connection", "Keep-Alive");
                urlConnection
                        .setRequestProperty("user-agent",
                                "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14)");
                urlConnection.connect();
                Map<String, List<String>> map = urlConnection
                        .getHeaderFields();

       
                bufferedReader = new BufferedReader(new InputStreamReader(
                        urlConnection.getInputStream()));
                String line;
                for (; (line = bufferedReader.readLine()) != null;) {
                    result += "\n" + line;
                }

            } catch (IOException e) {
                e.printStackTrace();

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bufferedReader) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        return result;

    }
}

Related Tutorials