Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android我如何从mainactivity调用这个类_Android - Fatal编程技术网

android我如何从mainactivity调用这个类

android我如何从mainactivity调用这个类,android,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 ja

如何从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调用它。首先,您需要一些公共方法,使它成为一个自身之外的可用类。将sendGET和sendPOST设置为public方法,而不是private方法

然后在主要活动中,获取:

HttpURLConnectionExample.sendGET();
张贴:

HttpURLConnectionExample.sendPOST();
这与调用任何其他类的方式相同,并且因为sendGET和sendPOST是静态的,所以在分配HttpURLConnectionExample的新对象时,您没有如下所示:

HttpURLConnectionExample httpURLConnectionExample = new HttpURLConnectionExample();

这是一个很常见的职业叫。就像你在其他java类中所做的那样,为什么这个标签是android?]当我使用HttpURLConnectionExample.sendGET时;表示sendGet在httpp.myapplication.httpurlConnectionExample中具有私人访问权限已更新我的答案。此外,在进行任何网络连接时,您必须在UIThread.nate之外进行连接。我仍然认为sendGet在httpp.myapplication.HttpURLConnectionExample中具有私有访问权限