android无法建立URL连接

时间:2014-03-21 22:04:23

标签: java android httpurlconnection

当我使用android 2.3.6(Ginger Bread)运行它时,我的应用程序正常运行,但是当我在4.2上运行它时,它确实使用了URL连接,它可以&# 39;建立连接 这是执行URL连接的代码片段:

try {
                Add.setEnabled(true);
                movieContent.setText("");
                String TITLE =searchET.getText().toString();
                TITLE = TITLE.replaceAll(" ", "%20");
                Log.d("title after parsing:", TITLE);
                URL url = new URL("http://www.omdbapi.com/?i=&t="
                        + TITLE);
                String URL2="http://www.omdbapi.com/?i=&t=saw";
                Log.d("URL content", url.toString());
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();
                Log.d("URL content", "register URL");
                urlConnection.connect();
                Log.d("URL connection", "establish connection");

日志cat显示流量到达"注册URL"记录打印然后以绿色标记为QCNEA的警告:

03-21 23:53:56.596: D/title after parsing:(16623): saw 03-21 23:53:56.596: D/URL content(16623): http://www.omdbapi.com/?i=&t=saw 03-21 23:53:56.596: D/URL content(16623): register URL 03-21 23:53:56.596: I/QCNEA(16623): |NIMS| getaddrinfo: hostname www.omdbapi.com servname NULL numeric 4 appname 03-21 23:53:56.616: I/MediaPlayer(16623): Don't send intent. msg.arg1 = 0, msg.arg2 = 0 03-21 23:53:57.207: V/MediaPlayer(16623): message received msg=2, ext1=0, ext2=0 03-21 23:53:57.207: V/MediaPlayer(16623): playback complete 03-21 23:53:57.207: V/MediaPlayer(16623): callback application 03-21 23:53:57.207: V/MediaPlayer(16623): back from callback

知道有关连接的所有权限都已添加。

奇怪的是没有错误,只是无法建立连接。

希望你们帮助我 提前谢谢

3 个答案:

答案 0 :(得分:1)

我怀疑你可能在主线程上遇到网络代码问题,但我不确定你为什么没有收到错误或应用程序崩溃。

将您的代码放入异步任务中,看它是否有效。

AsyncTask<Void, Void, Void> asyncLoad = new AsyncTask<Void, Void, Void>()
    {
        @Override
        protected Void doInBackground(Void... params)
        {
            URL url = new URL("http://www.omdbapi.com/?i=&t="
                    + TITLE);
            String URL2="http://www.omdbapi.com/?i=&t=saw";
            Log.d("URL content", url.toString());
            HttpURLConnection urlConnection = (HttpURLConnection) url
                    .openConnection();
            Log.d("URL content", "register URL");
            urlConnection.connect();
            Log.d("URL connection", "establish connection");

            return null;
        }

        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
        }
    };

    asyncLoad.execute();

答案 1 :(得分:0)

姜饼和更高版本之间的区别是关于主线程网络的严格政策。当您更改严格模式以允许所有允许游览活动在主线程上进行网络连接时...但您不应该这样做...您应该遵守规则并运行异步任务就像@JustSoAmazing建议的那样。

答案 2 :(得分:0)

首先,将以下内容附加到AndroidManifest.xml

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

其次,试试这个

int timeout = 15 * 1000; // timeout in 15 seconds 

HttpURLConnection urlConnection = (HttpURLConnection) url
                    .openConnection();

urlConnection.setConnectTimeout(timeout);
urlConnection.setReadTimeout(timeout);
urlConnection.connect();

// ...