Android http url连接有问题。 我有这个方法:
private String getHtml(String urlString) throws Exception {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
URL url = new URL(urlString);
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", "Basic " + password);
InputStream content = uc.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
pw.println(line);
}
return sw.toString();
}
当我从普通的Java应用程序中使用它时它可以正常工作。 但是,当我从Android应用程序中运行它时,它不起作用。
我观察到url连接是一个org.apache ...在Android中运行时的HttpURLConnection。当它使用它时,它不会进入while循环。
在普通的java应用程序中,它使用sun.net ... HttpURLConnection(可以工作)。
任何人都有任何关于如何在Android上使用它的建议吗?
答案 0 :(得分:-1)
你可以尝试这个解决方案:
try
{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
InputStream in = response.getEntity().getContent();
}
catch (Exception e)
{
// Code
}
目前无法记住正确的例外,但我认为你可以找到它。