我正在使用下面的代码,它不断给我malformaurlexception错误。有趣的部分是我从另一个相同的代码正常工作的项目中复制它。我想知道是否有人可以看到问题可能是什么。哪个是给错误代码的部分是OpenHttpConnection功能,在开始的地方说:java.net.malformedurlexception协议未找到
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;
你得到的URL错误。很明显,它不是以http://开头的。 – EJP
但我有硬编码的http://到urlString。怎样才能解决它? – Farshid
你在MalformedURLException上得到了什么? –