简体   繁体   English

android无法建立URL连接

[英]android can't establish URL connection

i've an application that was act properly when i run it with android 2.3.6(Ginger Bread), but when i run it on 4.2 it did'nt work with the URL connection, it can't establish the connection here is the snip of the code that performs the URL connection : 我有一个应用程序,当我用android 2.3.6(Ginger Bread)运行它时,行为正常,但是当我在4.2上运行它时,它与URL连接不起作用,它无法在此处建立连接执行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");

the log cat shows that the flow reach "register URL" log print it then raise a warning in green labeled as QCNEA: 日志猫显示该流到达“注册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

knowing that all permission regarding connection are added. 知道添加了有关连接的所有权限。

the weird thing that there is no error , just can't establish the connection. 没有错误的怪异事物,只是无法建立连接。

hope you guys help me with this thanks in advance 希望你们提前帮我

I suspect you might be running into issues with doing networking code on the main thread, though I'm not sure why you're not getting an error or app crash. 我怀疑您可能在主线程上执行网络代码时遇到了问题,尽管我不确定您为什么没有收到错误或应用程序崩溃。

Put your code in an Async task and see if it works. 将您的代码放在异步任务中,看看它是否有效。

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();

The difference between gingerbread and higher versions is the strict policy about networking on main thread. 姜饼和更高版本之间的区别是关于主线程上网络的严格策略。 When you change strict mode to permit all you allow tour activities to do that networking on main thread... But you shouldnt do that... You should go by the rules and run an async task Just like the one suggested by @JustSoAmazing. 当您更改严格模式以允许所有允许的巡回活动在主线程上进行网络连接时...但是您不应该这样做...您应遵循规则并运行异步任务,就像@JustSoAmazing建议的那样。

First, append following into AndroidManifest.xml 首先,将以下内容附加到AndroidManifest.xml中

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

Second, try this 第二,试试这个

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

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

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

// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Java和Android之间建立套接字连接 - Can't establish socket connection between Java and Android 无法在本地主机上建立与服务器的连接:8000 - can't establish a connection to the server at localhost:8000 无法与aSmack 4.0.2建立新连接 - Can't establish a new connection with aSmack 4.0.2 如何在Android Studio上使用SilverTunnel-NG建立与.onion URL的套接字连接? 有没有其他选择? - How can i establish a socket connection to an .onion url using SilverTunnel-NG on Android Studio? Is there any alternative? Eclipse Birt无法建立与derby数据库的连接 - Eclipse birt can't establish connection to derby database WebService:Firefox无法在192.168.10.203:8080建立与服务器的连接 - WebService: Firefox can't establish a connection to the server at 192.168.10.203:8080 Docker Cluster with Kafka 无法与客户端容器建立连接 - Docker Cluster with Kafka can't establish a connection with a client container Android:Activity建立网络连接 - Android: Activity establish network connection 由于没有互联网连接而无法打开网址时的android事件 - android event when url can't be opened because no internet connection 如何在android服务中建立WebSocket连接? - How to establish a WebSocket connection in android service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM