Java – Unhandled exception type URISyntaxException

Unhandled exception type URISyntaxException… here is a solution to the problem.

Unhandled exception type URISyntaxException

I’m trying to add an image from a URL to my Android app, but for some reason I’m getting an error message stating Unhandled exception type URISyntaxException. I believe I somehow formatted my code incorrectly and that I could use some help figuring out exactly what caused this I received” Unhandled exception type URISyntaxException” error.

Specific error line:

Unhandled exception type URISyntaxException: httpRequest = new HttpGet(imgUrl.toURI());

Java:

package com.example.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView tv;
    String url = "http://examplecloudurl.edu/apps/users/results.cfm?lname=FOO&fname=BAR";
    String tr;
    Document doc;

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

tv = (TextView) findViewById(R.id.TextView01);
        new MyTask().execute(url);
    }

private class MyTask extends AsyncTask<String, Void, String> {

ProgressDialog prog;

String title = "";

@Override
        protected void onPreExecute() {
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();

}

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

ImageView img = (ImageView) findViewById(R.id.imageView1);
            {
                try {
                    URL imgUrl = new URL(
                            "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg");

HttpGet httpRequest = null;

httpRequest = new HttpGet(imgUrl.toURI());

HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = (HttpResponse) httpclient
                            .execute(httpRequest);

HttpEntity entity = response.getEntity();
                    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
                    InputStream input = b_entity.getContent();

Bitmap bitmap = BitmapFactory.decodeStream(input);

img.setImageBitmap(bitmap);

doc = Jsoup.connect(params[0]).get();
                    Element tableElement = doc.select(".datagrid").first();

Elements tableRows = tableElement.select("tr");
                    for (Element row : tableRows) {
                        Elements cells = row.select("td");
                        if (cells.size() > 0) {
                            title = cells.get(0).child(0).attr("href") + " ; "
                                    + cells.get(0).text() + "; "
                                    + cells.get(1).text() + "; "
                                    + cells.get(2).text() + "; "
                                    + cells.get(3).text();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return title;
            }
        }

@Override
        protected void onPostExecute(String title) {
            super.onPostExecute(title);
            prog.dismiss();
            tv.setText(title);

}

}
}

Edited according to Reimeus:

public class MainActivity extends Activity {

TextView tv;
    String url = "http://examplecloudurl.edu/apps/users/results.cfm?lname=FOO&fname=BAR";
    String tr;
    Document doc;

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

tv = (TextView) findViewById(R.id.TextView01);
        new MyTask().execute(url);
    }

private class MyTask extends AsyncTask<String, Void, String> {

ProgressDialog prog;

String title = "";

@Override
        protected void onPreExecute() {
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();

}

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

ImageView img = (ImageView) findViewById(R.id.imageView1);
            {
                try {

String imageURL = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg";
                    HttpGet httpRequest = null;

httpRequest = new HttpGet(URI.create(imageURL));

HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = (HttpResponse) httpclient
                            .execute(httpRequest);

HttpEntity entity = response.getEntity();
                    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
                    InputStream input = b_entity.getContent();

Bitmap bitmap = BitmapFactory.decodeStream(input);

img.setImageBitmap(bitmap);

doc = Jsoup.connect(params[0]).get();
                    Element tableElement = doc.select(".datagrid").first();

Elements tableRows = tableElement.select("tr");
                    for (Element row : tableRows) {
                        Elements cells = row.select("td");
                        if (cells.size() > 0) {
                            title = cells.get(0).child(0).attr("href") + " ; "
                                    + cells.get(0).text() + "; "
                                    + cells.get(1).text() + "; "
                                    + cells.get(2).text() + "; "
                                    + cells.get(3).text();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return title;
            }
        }

@Override
        protected void onPostExecute(String title) {
            super.onPostExecute(title);
            prog.dismiss();
            tv.setText(title);

}

}
}

Lokat:

10-08 15:43:43.590: W/dalvikvm(4017): threadid=11: thread exiting with uncaught exception (group=0x40d01390)
10-08 15:43:43.790: E/WindowManager(4017): Activity com.example.test.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@413f0a88 that was originally added here
10-08 15:43:43.790: E/WindowManager(4017): android.view.WindowLeaked: Activity com.example.test.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$ DecorView@413f0a88 that was originally added here
10-08 15:43:43.790: E/WindowManager(4017):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
10-08 15:43:43.790: E/WindowManager(4017):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
10-08 15:43:43.790: E/WindowManager(4017):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
10-08 15:43:43.790: E/WindowManager(4017):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
10-08 15:43:43.790: E/WindowManager(4017):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
10-08 15:43:43.790: E/WindowManager(4017):  at android.app.Dialog.show(Dialog.java:278)
10-08 15:43:43.790: E/WindowManager(4017):  at com.example.test.MainActivity$MyTask.onPreExecute(MainActivity.java:54)
10-08 15:43:43.790: E/WindowManager(4017):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
10-08 15:43:43.790: E/WindowManager(4017):  at android.os.AsyncTask.execute(AsyncTask.java:511)
10-08 15:43:43.790: E/WindowManager(4017):  at com.example.test.MainActivity.onCreate(MainActivity.java:41)
10-08 15:43:43.790: E/WindowManager(4017):  at android.app.Activity.performCreate(Activity.java:4465)
10-08 15:43:43.790: E/WindowManager(4017):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-08 15:43:43.790: E/WindowManager(4017):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1923)
10-08 15:43:43.790: E/WindowManager(4017):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1984)
10-08 15:43:43.790: E/WindowManager(4017):  at android.app.ActivityThread.access$600(ActivityThread.java:126)
10-08 15:43:43.790: E/WindowManager(4017):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1150)
10-08 15:43:43.790: E/WindowManager(4017):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 15:43:43.790: E/WindowManager(4017):  at android.os.Looper.loop(Looper.java:137)
10-08 15:43:43.790: E/WindowManager(4017):  at android.app.ActivityThread.main(ActivityThread.java:4456)
10-08 15:43:43.790: E/WindowManager(4017):  at java.lang.reflect.Method.invokeNative(Native Method)
10-08 15:43:43.790: E/WindowManager(4017):  at java.lang.reflect.Method.invoke(Method.java:511)
10-08 15:43:43.790: E/WindowManager(4017):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
10-08 15:43:43.790: E/WindowManager(4017):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
10-08 15:43:43.790: E/WindowManager(4017):  at dalvik.system.NativeStart.main(Native Method)

Edited by Ravi Thapliyal:

public class MainActivity extends Activity {

TextView tv;
    String url = "http://examplecloudurl.edu/apps/users/results.cfm?lname=FOO&fname=BAR";
    String tr;
    Document doc;

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

tv = (TextView) findViewById(R.id.TextView01);
        new MyTask().execute(url);
    }

private class MyTask extends AsyncTask<String, Void, String> {

ProgressDialog prog;

String title = "";

@Override
        protected void onPreExecute() {
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();

}

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

ImageView img = (ImageView) findViewById(R.id.imageView1);
            {
                try {
                    URL imgUrl = new URL(
                            "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg");

HttpGet httpRequest = null;

httpRequest = new HttpGet( );

HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = (HttpResponse) httpclient
                            .execute(httpRequest);

HttpEntity entity = response.getEntity();
                    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
                    InputStream input = b_entity.getContent();

Bitmap bitmap = BitmapFactory.decodeStream(input);

img.setImageBitmap(bitmap);

doc = Jsoup.connect(params[0]).get();
                    Element tableElement = doc.select(".datagrid").first();

Elements tableRows = tableElement.select("tr");
                    for (Element row : tableRows) {
                        Elements cells = row.select("td");
                        if (cells.size() > 0) {
                            title = cells.get(0).child(0).attr("href") + " ; "
                                    + cells.get(0).text() + "; "
                                    + cells.get(1).text() + "; "
                                    + cells.get(2).text() + "; "
                                    + cells.get(3).text();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return title;
            }
        }

@Override
        protected void onPostExecute(String title) {
            super.onPostExecute(title);
            prog.dismiss();
            tv.setText(title);

}

}
}

Lokat:

10-08 15:40:32.970: E/WindowManager(3901): Activity com.example.test.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@413eeed8 that was originally added here
10-08 15:40:32.970: E/WindowManager(3901): android.view.WindowLeaked: Activity com.example.test.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$ DecorView@413eeed8 that was originally added here
10-08 15:40:32.970: E/WindowManager(3901):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
10-08 15:40:32.970: E/WindowManager(3901):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
10-08 15:40:32.970: E/WindowManager(3901):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
10-08 15:40:32.970: E/WindowManager(3901):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
10-08 15:40:32.970: E/WindowManager(3901):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
10-08 15:40:32.970: E/WindowManager(3901):  at android.app.Dialog.show(Dialog.java:278)
10-08 15:40:32.970: E/WindowManager(3901):  at com.example.test.MainActivity$MyTask.onPreExecute(MainActivity.java:53)
10-08 15:40:32.970: E/WindowManager(3901):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
10-08 15:40:32.970: E/WindowManager(3901):  at android.os.AsyncTask.execute(AsyncTask.java:511)
10-08 15:40:32.970: E/WindowManager(3901):  at com.example.test.MainActivity.onCreate(MainActivity.java:40)
10-08 15:40:32.970: E/WindowManager(3901):  at android.app.Activity.performCreate(Activity.java:4465)
10-08 15:40:32.970: E/WindowManager(3901):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-08 15:40:32.970: E/WindowManager(3901):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1923)
10-08 15:40:32.970: E/WindowManager(3901):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1984)
10-08 15:40:32.970: E/WindowManager(3901):  at android.app.ActivityThread.access$600(ActivityThread.java:126)
10-08 15:40:32.970: E/WindowManager(3901):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1150)
10-08 15:40:32.970: E/WindowManager(3901):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 15:40:32.970: E/WindowManager(3901):  at android.os.Looper.loop(Looper.java:137)
10-08 15:40:32.970: E/WindowManager(3901):  at android.app.ActivityThread.main(ActivityThread.java:4456)
10-08 15:40:32.970: E/WindowManager(3901):  at java.lang.reflect.Method.invokeNative(Native Method)
10-08 15:40:32.970: E/WindowManager(3901):  at java.lang.reflect.Method.invoke(Method.java:511)
10-08 15:40:32.970: E/WindowManager(3901):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
10-08 15:40:32.970: E/WindowManager(3901):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
10-08 15:40:32.970: E/WindowManager(3901):  at dalvik.system.NativeStart.main(Native Method)

Solution

The error is self-explanatory

Unhandled exception type URISyntaxException

Because URISyntaxException is a checked exception, you also need to catch it.

} catch (URISyntaxException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Related Problems and Solutions