不能在我的Andr​​oid应用程序连接到互联网只。每种方法连接互联网返回空

编程入门 行业动态 更新时间:2024-10-23 07:19:39
本文介绍了不能在我的Andr​​oid应用程序连接到互联网只。每种方法连接互联网返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 HttpURLConnection类的连接方法,但它返回空。没有其他的消息没有printstack只是空。

I am using HttpURLConnection's connect method but it returns null. no other message no printstack just null.

URL url = new URL("whatever"); URLConnection urlConnection = url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream());

在上面也返回空

不知道为什么会这样来了。但我无法连接到网络。

Don't know why this comes. But I cant connect to net.

我也试图与HttpURLConnection类和URLConnection都是不同的例子。

i also tried with HttpURLConnection and urlconnection both are different example.

推荐答案

您说您正在使用 HttpURLConnection类但在code你波斯特的URLConnection 。这是你如何作出正确的 HttpURLConnection类:

You said you are using HttpURLConnection but in the code you poste URLConnection. This is how you make a proper HttpURLConnection:

URL url = new URL("www.android/"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in); finally { urlConnection.disconnect(); } }

您还必须做在主线程超出这个连接(使用的AsyncTask 或其他方式)或其他应用程序将崩溃。

You also have to do this connection outside the main thread (Using AsyncTask or other method) or otherwise the app will crash.

另外添加正确的权限是:

Also add the right permissions for it:

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

希望它帮助!祝你好运!

Hope it helps! Good Luck!

更多推荐

不能在我的Andr​​oid应用程序连接到互联网只。每种方法连接互联网返回空

本文发布于:2023-11-27 20:10:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639435.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:互联网   在我   连接到   应用程序   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!