我正在使用以下代码,它不断给我 malformedurlexception 错误。有趣的是我从另一个项目中复制了它,相同的代码工作正常。我想知道是否有人可以看到问题可能是什么。给出错误的代码部分在 OpenHttpConnection 函数中,并在开始时显示:
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
我很感激我能得到的任何帮助。
Bitmap bitmapOrg = null;
bitmapOrg = DownloadImage("http://www.bagherpour.com/apache_pb.gif");
public Bitmap DownloadImage(String txt)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(txt);
bitmap = BitmapFactory.decodeStream(in);
in.close();
return bitmap;
} catch (IOException e1) {
e1.printStackTrace();
Log.d("bitMap",e1.toString());
return null;
}
}
public InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;