0

URLに接続するための次のコードがあります。私の問題は、デバッグで実行するとconnected = false、設定しようとしている他のパラメーターが機能していないことがわかります(などtimeout)。誰でもこれで私を助けることができますか?私は何を間違っていますか??

誰かが尋ねる前に、私のマニフェストにはインターネット アクセス許可があります。

private static class SyncOperation extends AsyncTask<String, String, Integer> {

    @Override
    protected Integer doInBackground(String... params) {
        HttpsURLConnection urlConnection = null;
        URL url = null;
        String msg = "Good";
        int code = 0;
        try {
            System.setProperty("http.keepAlive", "false");
            url = new URL("https://www.google.com");
            urlConnection =  (HttpsURLConnection) url.openConnection();
            urlConnection.setInstanceFollowRedirects(false);
            urlConnection.setReadTimeout(10000 /* milliseconds */);
            urlConnection.setConnectTimeout(15000 /* milliseconds */);
            urlConnection.setRequestMethod("POST");
        } catch (MalformedURLException e) {
            msg = "Bad MalformedURLException";
            e.printStackTrace();
            Log.e("HTTPS", e.getMessage());
        } catch (IOException e) {
            msg = "Bad IOException";
            e.printStackTrace();
            Log.e("HTTPS", e.getMessage());
        } finally {
            urlConnection.disconnect();
            Log.d("HTTPS", msg);
            Log.d("HTTPS", "diconnected");
        }

        return code;
    }

    @Override
    protected void onPostExecute(Integer code) {
        super.onPostExecute(code);

        Toast t = Toast.makeText(mContext, code.toString(), Toast.LENGTH_SHORT);
        t.show();

    }

}
4

1 に答える 1

2

接続を構成しているだけで、接続していません。メソッドの名前url.openConnection()は紛らわしいです: 接続を開きません。最初は接続されていない接続オブジェクトのみが提供されます。

connect()など、接続を確立するメソッドを呼び出す必要があります。

つまり、コンテンツの読み取りや への書き込みなど、接続を使用して実際に何かを実行してみてくださいSystem.out.println()。これにより、接続が自動的に開きます。

于 2013-04-29T15:35:58.233 に答える