MobiusApps
MobiusApps

Reputation: 73

MalformedURLException: Protocol not found using Jsoup

Hello I'm using Jsoup to parse an image from this website http://www.izismile.com/ to android.

I use this jsoup sample http://www.androidbegin.com/tutorial/android-basic-jsoup-tutorial/

When i use his own site, it works. but when i change the url to mine, it doesn't work.

Does anyone had this issue?

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)

and this is my code

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();
    }
}

Upvotes: 1

Views: 1471

Answers (2)

Pphoenix
Pphoenix

Reputation: 1473

The problem is that the source you are trying to retrieve is not a full path, it only returns /templates/960x/images/logo_izismile.gif, instead of http://www.izismile.com/templates/960x/images/logo_izismile.gif. This causes your new URL(imgSrc).openStream();to throw an exception since the relative path doesn't make sense to it.

@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;
}

Upvotes: 2

Boe-Dev
Boe-Dev

Reputation: 1595

I work with this:

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);
    }
}

Upvotes: 0

Related Questions