MalformedURLException:使用Jsoup找不到协议

时间:2014-04-14 08:13:26

标签: java android url jsoup

您好我使用Jsoup将此网站http://www.izismile.com/中的图片解析为安卓。

我使用这个jsoup示例http://www.androidbegin.com/tutorial/android-basic-jsoup-tutorial/

当我使用自己的网站时,它有效。但是当我将网址更改为我的网址时,它无法正常工作。

有人有这个问题吗?

04-14 10:06:33.807: W/System.err(27635): java.net.MalformedURLException: Protocol not found: /templates/960x/images/logo_izismile.gif
04-14 10:06:33.807: W/System.err(27635):    at java.net.URL.<init>(URL.java:176)
04-14 10:06:33.807: W/System.err(27635):    at java.net.URL.<init>(URL.java:125)
04-14 10:06:33.807: W/System.err(27635):    at com.androidbegin.jsouptutorial.MainActivity$Logo.doInBackground(MainActivity.java:69)
04-14 10:06:33.807: W/System.err(27635):    at com.androidbegin.jsouptutorial.MainActivity$Logo.doInBackground(MainActivity.java:1)
04-14 10:06:33.807: W/System.err(27635):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-14 10:06:33.807: W/System.err(27635):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-14 10:06:33.807: W/System.err(27635):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-14 10:06:33.807: W/System.err(27635):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-14 10:06:33.807: W/System.err(27635):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-14 10:06:33.807: W/System.err(27635):    at java.lang.Thread.run(Thread.java:841)

这是我的代码

public class MainActivity extends Activity {

String url = "http://www.izismile.com";
ProgressDialog mProgressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button logobutton = (Button) findViewById(R.id.button1);

    logobutton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            new Logo().execute();
        }
    });

}

public static Connection connect(String url) {
    return HttpConnection.connect(url);
}

private class Logo extends AsyncTask<Void, Void, Void> {
    Bitmap bitmap;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setTitle("Android Basic JSoup Tutorial");
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        try {
            Document document = Jsoup.connect(url).get();
            Elements img = document.select("div[class=logo_top] img[src]");
            String imgSrc = img.attr("src");
            InputStream input = new URL(imgSrc).openStream();
            bitmap = BitmapFactory.decodeStream(input);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        ImageView logoimg = (ImageView) findViewById(R.id.logo);
        logoimg.setImageBitmap(bitmap);
        mProgressDialog.dismiss();
    }
}

2 个答案:

答案 0 :(得分:1)

问题是您尝试检索的来源不是完整路径,只返回/templates/960x/images/logo_izismile.gif,而不是http://www.izismile.com/templates/960x/images/logo_izismile.gif。这导致您的new URL(imgSrc).openStream();抛出异常,因为相对路径对它没有意义。

@Override
protected Void doInBackground(Void... params) {

    try {
        Document document = Jsoup.connect(url).get();
        Elements img = document.select("div[class=logo_top] img[src]");
        String imgSrc = img.attr("src");
        // imgSrc may not be full path
        if(!imgSrc.startsWith(url)) {
            // create full path
            imgSrc = url+imgSrc;
        }
        InputStream input = new URL(imgSrc).openStream();
        bitmap = BitmapFactory.decodeStream(input);

    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

答案 1 :(得分:0)

我使用这个:

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}