我目前正在尝试通过 http Get 调用对服务器进行身份验证。下面提供的代码在 java 项目中编译时可以工作。将正确的令牌返回给程序。但是,每当我尝试在 Android 中实现相同的代码时,我都不会通过 Get 调用返回令牌。
在 Android 中,我在函数中返回 inputLine,但 inputLine 始终是空字符串。
system.out.println() 打印返回的令牌。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class JavaHttpsExample
{
public static void main(String[] args)
{ String inputLine = new String();
try
{
String httpsURL = "https://the url";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
InputStream ins = con.getInputStream();
InputStreamReader isr=new InputStreamReader(ins);
BufferedReader in =new BufferedReader(isr);
inputLine = in.readLine();
System.out.println(inputLine);
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
感谢您的帮助!!!
您可能没有将 Internet-Permission 添加到您的项目 AndroidManifest.xml 中。
如果是这样,请将以下行添加为<manifest/>
node:
<uses-permission android:name="android.permission.INTERNET" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)