我正在尝试将URL中的图像添加到我的android应用程序中,但是由于某种原因,我收到一条错误消息,指出Unhandled exception type URISyntaxException.
,我认为我以某种方式对我的代码进行了格式错误,并且我可以使用一些帮助来弄清楚到底是什么原因我收到此“未处理的异常类型URISyntaxException”错误。
特定错误行:
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);
}
}
}
针对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)
针对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)
最佳答案
错误是不言自明的
Unhandled exception type URISyntaxException
由于
URISyntaxException
是检查的例外,因此您也需要对其进行catch
。} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}