android如何从mainactivity调用此类

时间:2015-08-19 01:14:35

标签: android

如何从mainactivity调用此类?任何人都可以帮助我,因为我正在努力学习,如果你可以帮我解决这个问题,我会感激不尽。

package httpp.myapplication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpURLConnectionExample {

    private static final String USER_AGENT = "Mozilla/5.0";

    private static final String GET_URL = "file:///C:/2-click%20run/index.html";

    private static final String POST_URL = "file:///C:/2-click%20run/home.html";

    private static final String POST_PARAMS = "userName=Pankaj";

    public static void main(String[] args) throws IOException {

        sendGET();
        System.out.println("GET DONE");
        sendPOST();
        System.out.println("POST DONE");
    }

    private static void sendGET() throws IOException {
        URL obj = new URL(GET_URL);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", USER_AGENT);
        int responseCode = con.getResponseCode();
        System.out.println("GET Response Code :: " + responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result
            System.out.println(response.toString());
        } else {
            System.out.println("GET request not worked");
        }

    }

    private static void sendPOST() throws IOException {
        URL obj = new URL(POST_URL);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);

        // For POST only - START
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        // For POST only - END

        int responseCode = con.getResponseCode();
        System.out.println("POST Response Code :: " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) { //success
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result
            System.out.println(response.toString());
        } else {
            System.out.println("POST request not worked");
        }
    }

}

我不确定如何从MainActivity中调用它

1 个答案:

答案 0 :(得分:0)

首先,您需要使用某种公共方法,使其成为一个可用的类。将<?php $host = "host"; $db_username = "username"; $db_password = ""; $db_database = "database"; $db = mysqli_connect($host,$db_username,$db_password,$db_database) or die("Errors"); ?> sendGET()设置为公共方法,而不是私有。

然后在您的MainActivity中获取:

sendPOST()

发布:

HttpURLConnectionExample.sendGET();

这与调用任何其他类的方式相同,因为sendGET和sendPOST是静态的,你没有在HttpURLConnectionExample中分配一个新对象,如:

HttpURLConnectionExample.sendPOST();