【发布时间】:2011-03-06 21:39:32
【问题描述】:
我目前正在尝试通过 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();
}
}
}
感谢您的帮助!!!
【问题讨论】:
-
你能看到服务器端发生了什么吗?访问日志、状态码等
-
很遗憾没有,服务器托管在外面
-
当我在 Android 中运行代码时,它似乎总是在“InputStream ins - con.getInputStream();”行失败它抛出 IOException。但是,当我将代码作为 java 应用程序运行时,这不会发生。
-
你能发布堆栈跟踪吗?