6

我正在尝试将来自 URL 的图像添加到我的 android 应用程序,但由于某种原因,我收到一条错误消息,指出Unhandled exception type URISyntaxException.我认为我的代码格式错误,我可以使用一些帮助来找出究竟是什么导致了这种情况我得到“未处理的异常类型 URISyntaxException”错误。

具体错误行:

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

爪哇:

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

        }

    }
}

编辑回应 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);

        }

    }
}

日志猫:

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)

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

        }

    }
}

日志猫:

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

2 回答 2

2

错误是不言自明的

Unhandled exception type URISyntaxException

因为,URISyntaxException是一个检查异常,你也需要catch它。

} catch (URISyntaxException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
于 2013-10-08T19:09:23.303 回答
0

为什么不直接使用URI#create

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

它不会引发检查异常,并且可以在您知道 URL 具有合法语法时使用

于 2013-10-08T19:15:50.460 回答