gpt4 book ai didi

java - Caused by : java. lang.IllegalArgumentException: 主机名可能不为空

转载 作者:搜寻专家 更新时间:2023-11-01 08:48:11 26 4
gpt4 key购买 nike

我在我的代码中使用了四个 字符串first 字符串包含“http://”,second 字符串包含用户主机 URL(由用户输入),third 包含“/musics/a.mp3”,第四个 字符串包含完整的 URL,通过组合前面的字符串,如下所示:

String fourth = first+second+third;

然后我在 AsyncTask 的执行中使用 fourth 字符串,如下所示:

new JSONAsyncTask().execute(fourth);

我收到以下错误。

日志:

10-07 06:17:15.934: E/AndroidRuntime(1194): FATAL EXCEPTION: AsyncTask #1
10-07 06:17:15.934: E/AndroidRuntime(1194): java.lang.RuntimeException: An error occured while executing doInBackground()
10-07 06:17:15.934: E/AndroidRuntime(1194): at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-07 06:17:15.934: E/AndroidRuntime(1194): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.lang.Thread.run(Thread.java:856)
10-07 06:17:15.934: E/AndroidRuntime(1194): Caused by: java.lang.IllegalArgumentException: Host name may not be null
10-07 06:17:15.934: E/AndroidRuntime(1194): at org.apache.http.HttpHost.<init>(HttpHost.java:83)
10-07 06:17:15.934: E/AndroidRuntime(1194): at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
10-07 06:17:15.934: E/AndroidRuntime(1194): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-07 06:17:15.934: E/AndroidRuntime(1194): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-07 06:17:15.934: E/AndroidRuntime(1194): at com.app.ifko.HomeActivity$JSONAsyncTask.doInBackground(HomeActivity.java:101)
10-07 06:17:15.934: E/AndroidRuntime(1194): at com.app.ifko.HomeActivity$JSONAsyncTask.doInBackground(HomeActivity.java:1)
10-07 06:17:15.934: E/AndroidRuntime(1194): at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-07 06:17:15.934: E/AndroidRuntime(1194): ... 5 more
10-07 06:17:22.764: E/WindowManager(1194): Activity com.app.ifko.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416b80b0 that was originally added here
10-07 06:17:22.764: E/WindowManager(1194): android.view.WindowLeaked: Activity com.app.ifko.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416b80b0 that was originally added here
10-07 06:17:22.764: E/WindowManager(1194): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:343)
10-07 06:17:22.764: E/WindowManager(1194): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:245)
10-07 06:17:22.764: E/WindowManager(1194): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
10-07 06:17:22.764: E/WindowManager(1194): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
10-07 06:17:22.764: E/WindowManager(1194): at android.view.Window$LocalWindowManager.addView(Window.java:537)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.Dialog.show(Dialog.java:274)
10-07 06:17:22.764: E/WindowManager(1194): at com.app.ifko.HomeActivity$JSONAsyncTask.onPreExecute(HomeActivity.java:90)
10-07 06:17:22.764: E/WindowManager(1194): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
10-07 06:17:22.764: E/WindowManager(1194): at android.os.AsyncTask.execute(AsyncTask.java:511)
10-07 06:17:22.764: E/WindowManager(1194): at com.app.ifko.HomeActivity.onCreate(HomeActivity.java:74)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.Activity.performCreate(Activity.java:4465)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-07 06:17:22.764: E/WindowManager(1194): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 06:17:22.764: E/WindowManager(1194): at android.os.Looper.loop(Looper.java:137)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-07 06:17:22.764: E/WindowManager(1194): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 06:17:22.764: E/WindowManager(1194): at java.lang.reflect.Method.invoke(Method.java:511)
10-07 06:17:22.764: E/WindowManager(1194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-07 06:17:22.764: E/WindowManager(1194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-07 06:17:22.764: E/WindowManager(1194): at dalvik.system.NativeStart.main(Native Method)
10-07 06:17:24.144: I/Process(1194): Sending signal. PID: 1194 SIG: 9

Activity 代码:

public class HomeActivity extends Activity {

String first, second, third, fourth;
TextView etTextToSave;
private SharedPreferences sharedprefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.home);

etTextToSave = (TextView) findViewById(R.id.etTextToSave);

sharedprefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
etTextToSave.setText(sharedprefs.getString("SharedPrefsData",""));

second = etTextToSave.getText().toString();

first = "http://";
third = "/musics/a.mp3";
fourth = first+second+third;

.........................

new JSONAsyncTask().execute(fourth);

}

最佳答案

如果 URL 的主机部分来自用户,您必须在使用它构建 URL 之前对其进行验证。

如果 second 为 null,则不要调用 new JSONAsyncTask().execute(fourth);

关于java - Caused by : java. lang.IllegalArgumentException: 主机名可能不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26230042/

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