Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled exception type URISyntaxException

I'm attempting to add images 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 have misformatted my code somehow and I could use a bit of assistance figuring out what exactly might be causing this "Unhandled exception type URISyntaxException" error I'm getting.

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

        }

    }
}

EDIT IN RESPONSE 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);

        }

    }
}

LOGCAT:

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)

EDIT IN RESPONSE TO 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);

        }

    }
}

LOGCAT:

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)
like image 935
HelloMojo Avatar asked Oct 08 '13 18:10

HelloMojo


People also ask

What causes URISyntaxException?

URISyntaxException . This is a checked exception that occurs when you are trying to parse a string that represents a URI, but it doesn't have the correct format.

What is Java net URISyntaxException?

java.net.URISyntaxException. Checked exception thrown to indicate that a string could not be parsed as a URI reference.

What is unhandled exception type?

An exception is a known type of error. An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist.


1 Answers

The error is self-explanatory

Unhandled exception type URISyntaxException

Since, URISyntaxException is a checked exception you need to catch it as well.

} catch (URISyntaxException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
like image 142
Ravi K Thapliyal Avatar answered Sep 29 '22 05:09

Ravi K Thapliyal