|安卓|如何将httpsconnection连接到php

时间:2018-01-15 22:48:52

标签: php android android-studio https

我对Android HttpsURLConnection有疑问(我从未使用过2台计算机之间的连接,只是简单的客户端)。问题是我无法连接到urlconnection.connect,例如,我在发布此帖之前试图找到asnwer,但我总是得到相同的答案而且从未奏效。

我的代码很简单,但我无法弄清楚如何解决它:

    protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    try {
        URL url= new  URL("https://example.com/api.php?uid=test1&pwd=12345");
        HttpsURLConnection urlConnection= (HttpsURLConnection) url.openConnection();
        urlConnection.setConnectTimeout(5000);
        urlConnection.setReadTimeout(5000);
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.connect(); // Here is where the error occurs
        int i= urlConnection.getResponseCode(); // Here is where the error occurs too
        System.err.println(i);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

是因为https? 顺便说一句,“api.php”文件的结果总是(我没有制作那个文件)

{"Result":1}

我无法从文件中获得该结果。请帮帮我,谢谢。

2 个答案:

答案 0 :(得分:0)

请尝试以下代码。

将API变量更改为基于Web的API的基本URL,发出类似的请求。

String myResult = Connection.Request(" SubDirectory / MyFile.php"," MyData");

您的PHP填写将

$_POST["Data"];

捕获数据进行处理。我希望这有帮助!

public class Connection {
    public static String Request(String UseURL, String data) {
        String API = "http://www.example.com/API/";
        try {
                    String data = "Data=" + URLEncoder.encode(data, "UTF-8");
                    URL postURL = new URL(API + UseURL);
                    HttpURLConnection conn = (HttpURLConnection) postURL.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setDoOutput(true);
                    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
                    writer.write(data);
                    writer.flush();
                    String line;
                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String retData = "";
                    while ((line = reader.readLine()) != null) {
                      retData += line;
                    }
                    writer.close();
                    reader.close();
                    return retData;
        } catch (Exception e) {
            return "Error Connecting to Server: " + e;
        }
    }
}

答案 1 :(得分:0)

您不会向我们提供有关运行代码时所获得的日志的信息。我在我的Android Studio上尝试你的代码,没有错误。

您是否在AndroidManifest.xml文件中添加了访问互联网的权限?

<uses-permission android:name="android.permission.INTERNET"/>

如果不是,请在您的清单上添加以上代码。

您在主线程上运行网络操作。只需在代码之前添加以下代码:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

我想你得到的错误是关于android.os.NetworkOnMainThreadException。所以,整个代码都是这样的:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {
            URL url= new  URL("https://kamus.nusagates.com");
            HttpsURLConnection urlConnection= (HttpsURLConnection) url.openConnection();
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.connect(); 
            int i= urlConnection.getResponseCode(); 
            System.err.println(i);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

除了上面的代码,你会给你回复200。