找不到java.net.malformedurlexception协议
我正在使用下面的代码,它不断地给我提供畸形的DurLexception错误。有趣的是,我从另一个项目复制了它,同样的代码工作正常。我想知道是否有人能看出问题出在哪里。给出错误的代码部分位于OpenHttpConnection函数中,并且在开始处显示:找不到java.net.malformedurlexception协议,java,http,protocols,malformedurlexception,Java,Http,Protocols,Malformedurlexception,我正在使用下面的代码,它不断地给我提供畸形的DurLexception错误。有趣的是,我从另一个项目复制了它,同样的代码工作正常。我想知道是否有人能看出问题出在哪里。给出错误的代码部分位于OpenHttpConnection函数中,并且在开始处显示: URL url = new URL(urlString); URLConnection conn = url.openConnection(); 我很感激能得到的任何帮助 Bitmap bitmapOrg = null; bitmapOrg =
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 rul = new URL("httpwww.bagherpour.com/apache_pb.gif")
你的网址错了。很明显,它根本不是以http://开头的,但我已经将http://硬编码到urlString中。如何才能正确处理它?您在哪一行获得了错误的异常?我将代码更改为URL URL=newurl(“http”,“www.bagherpour.com”,“apache_pb.gif”);然后MalformedURLException消失了,但我仍然无法连接。它表示W/System.err(968):java.io.IOException:连接错误。在执行httpConn.connect()时会发生这种情况;改变什么?还有,请解释原因。这是垃圾。它只会给你另一个格式错误的URL。