gpt4 book ai didi

java - 未处理的异常类型 URISyntaxException

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:15 28 4
gpt4 key购买 nike

我正在尝试将来自 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);

}

}
}

洛卡特:

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)

最佳答案

错误是不言自明的

Unhandled exception type URISyntaxException

因为 URISyntaxException 是一个checked 异常,您也需要捕获它。

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

关于java - 未处理的异常类型 URISyntaxException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19255821/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com