SoFunction
Updated on 2025-04-10

Android uses url connection example (get and post data to get return data)


package com.;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

/**
* Used for get or post data
 */
public class GetPostUtil {
    public static final String TAG = "GetPostUtil Debug";

    /**
     * @param url
*
* @return Return the data after get
     */
    public static String sendGet(String url) {
        String result = "";
        // String
        URL realURL = null;
        URLConnection conn = null;
        BufferedReader bufReader = null;
        String line = "";
        try {
            realURL = new URL(url);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            ();
("url format error");
        }

        try {
            conn = ();
// Set connection parameters...("xx", "xx");

            (10000); // 10s timeout
            // ("accept", "*/*");
            // ("", "");

(); // The connection sends the parameters out of the get method

        } catch (IOException e) {
            // TODO Auto-generated catch block
            ();
("Connection Error");
        }

        try {
            bufReader = new BufferedReader(new InputStreamReader(
                    (), "gb2312"));

            while ((line = ()) != null) {
                result += line + "\n";
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            ();
("Read data error");
        } finally {
// Release resources
            if (bufReader != null) {
                try {
                    ();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    ();
                }
            }
        }

        return result;
    }

    /**
     * @param url
     * @param param
*
     * @return
     */
    public static String sendGet(String url, String param) {
        return sendGet(url + "?" + param);
    }

    /**
     * @param url
*
     * @param param
*
* @return Return the data after post
     */
    public static String sendPost(String url, String param) {
        String result = "";
        URL realURL = null;
        BufferedReader bufReader = null;
        // PrintWriter printWriter = null;
        PrintStreamPrinter out = null;
        URLConnection connection = null;
        String line = "";
        try {
            realURL = new URL(url);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            ();
        }
        try {
            connection = ();
// Set to input and output post mode, and the input data cannot be obtained before output, otherwise an error will be reported
            (true);
            (true);

// It has been connected, so you can't use connect() anymore, otherwise an error will be reported

            out = new PrintStreamPrinter(new PrintStream(
                    ()));
            (param);
            //
        } catch (IOException e) {
            // TODO Auto-generated catch block
            ();
        }

        try {
            bufReader = new BufferedReader(new InputStreamReader(
                    (), "gb2312"));
            while ((line = ()) != null) {
                result += line + "\n";
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            ();
        } finally {
// Release resources
            try {
                if (bufReader != null) {
                    ();
                }
                if (out != null) {
                    //
                }
            } catch (IOException e2) {
                // TODO: handle exception
            }

        }
        return result;
    }

    public static void saveFile(String content) {

        File file = new File(()
                .getAbsolutePath(), "");
        if (!()) {
            try {
                boolean status = ();

                ("is create new file :" + status);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                ();
            }
        }
        PrintWriter pw = null;
        try {
            FileWriter fw = new FileWriter(file);
            // pw = new PrintWriter(new Date() + ".html");
            // (content);

            (content);
            ();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            ();
            if (pw != null) {
                ();
            }
        }
    }

}