Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
can';t仅在我的android应用程序中才能连接到internet。连接internet的每个方法都返回null_Android_Url_Null_Httpurlconnection - Fatal编程技术网

can';t仅在我的android应用程序中才能连接到internet。连接internet的每个方法都返回null

can';t仅在我的android应用程序中才能连接到internet。连接internet的每个方法都返回null,android,url,null,httpurlconnection,Android,Url,Null,Httpurlconnection,我正在使用HttpURLConnection的connect方法,但它返回null。 没有其他消息,没有打印堆栈,只有null URL url = new URL("http://whatever"); URLConnection urlConnection = url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 在上面的示例中,它还返回空值 不知道为什么

我正在使用
HttpURLConnection
的connect方法,但它返回
null
。 没有其他消息,没有打印堆栈,只有null

URL url = new URL("http://whatever");
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
在上面的示例中,它还返回空值

不知道为什么会这样。但是我不能上网


我还尝试了HttpURLConnection和urlconnection,这两个例子都不同。

在清单文件中添加以下权限-

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

您说您正在使用
HttpURLConnection
,但在您发布的代码中
URLConnection
。这就是如何建立正确的
HttpURLConnection

URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
  InputStream in = new BufferedInputStream(urlConnection.getInputStream());
  readStream(in);
 finally {
  urlConnection.disconnect();
 }
}
您还必须在主线程之外进行此连接(使用
AsyncTask
或其他方法),否则应用程序将崩溃

还要为其添加正确的权限:

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


希望有帮助!祝你好运

您是否已将权限添加到清单中?抱歉,我同时使用urlconnection和httpurlconnection进行了测试。这两个都是不同的示例。logcat中有任何错误或警告吗?在我使用的try catch块getmessage()中发布您的logcat infono,它只打印空值