我正在尝试从我的 android 应用程序访问 IP 地址,但我没有任何运气。这是我正在尝试的异步任务:
protected String doInBackground(Void...arg0) {
try{
URL url = new URL(myIP);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
String read = readStream(con.getInputStream());
return read;
} catch(Exception e){
e.printStackTrace();
}
return null;
}
private String readStream(InputStream in){
BufferedReader reader = null;
StringBuilder sb = new StringBuilder("xxxx");
try{
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while((line = reader.readLine()) != null){
sb.append(line + "\n");
}
} catch(IOException e){
e.printStackTrace();
} finally{
if(reader != null){
try{
reader.close();
} catch(IOException e){
e.printStackTrace();
}
}
}
return sb.toString();
}
protected void onPostExecute(String result){
textView.setText(result);
}
当我使用像 www.stackoverflow.com 这样的普通 URL 时,它可以正常工作,但是当我尝试使用 IP 时,却没有任何响应。它没有抛出任何异常,也没有更新 textView(我尝试根据抛出的异常返回不同的结果)。所以这让我想到了我的问题,IP 地址是 URL 连接的有效输入吗?让我知道我是否可以提供任何其他信息。
我的最终目标是将 REST 请求发送到此 IP 以访问特定信息也可能会有所帮助。