gpt4 book ai didi

android - httpClient.execute(httpPost) 导致崩溃 (Android 4.1)

转载 作者:行者123 更新时间:2023-11-30 03:57:41 26 4
gpt4 key购买 nike

也许你可以帮我。我正在开发一个调用 Rotten Tomatoes API 的 Android 应用程序项目,该 API 返回 JSON 数据。但是每当我运行我的代码时,应用程序就会崩溃。给我带来问题的具体代码是 httpClient.execute(httpPost);

传递给方法的 url 字符串没有问题,当我在浏览器中键入它时会返回正确的数据,所以我知道这不是问题所在。我有点不知所措。我从来没有遇到过 HttpClient 的问题,尽管过去我只使用 XML,从不使用 JSON。

我已经为此苦苦挣扎了一段时间。如果有人有任何见解,将不胜感激。

 public JSONObject getJSON(String url) {
//Make HTTP Request
try {

//defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
//Log.d("RunTest", url);
HttpResponse httpResponse = httpClient.execute(httpPost); // THIS LINE CAUSES APPLICATION TO CRASH
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} catch (UnsupportedEncodingException uee) {
Log.d("Exceptions", "UnsupportedEncodingException");
uee.printStackTrace();
} catch (ClientProtocolException cpe) {
Log.d("Exceptions", "ClientProtocolException");
cpe.printStackTrace();
} catch (IOException ioe) {
Log.d("Exceptions", "IOException");
ioe.printStackTrace();
}

10-27 02:52:44.484: I/Choreographer(627): Skipped 147 frames! The application may be doing too much work on its main thread.
10-27 02:52:44.704: W/dalvikvm(627): threadid=12: thread exiting with uncaught exception (group=0x40a13300)
10-27 02:52:44.724: E/AndroidRuntime(627): FATAL EXCEPTION: AsyncTask #1
10-27 02:52:44.724: E/AndroidRuntime(627): java.lang.RuntimeException: An error occured while executing doInBackground()
10-27 02:52:44.724: E/AndroidRuntime(627): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-27 02:52:44.724: E/AndroidRuntime(627): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-27 02:52:44.724: E/AndroidRuntime(627): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-27 02:52:44.724: E/AndroidRuntime(627): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-27 02:52:44.724: E/AndroidRuntime(627): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-27 02:52:44.724: E/AndroidRuntime(627): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-27 02:52:44.724: E/AndroidRuntime(627): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-27 02:52:44.724: E/AndroidRuntime(627): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-27 02:52:44.724: E/AndroidRuntime(627): at java.lang.Thread.run(Thread.java:856)
10-27 02:52:44.724: E/AndroidRuntime(627): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json
10-27 02:52:44.724: E/AndroidRuntime(627): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
10-27 02:52:44.724: E/AndroidRuntime(627): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
10-27 02:52:44.724: E/AndroidRuntime(627): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-27 02:52:44.724: E/AndroidRuntime(627): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-27 02:52:44.724: E/AndroidRuntime(627): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-27 02:52:44.724: E/AndroidRuntime(627): at com.example.rottentomatoesclient.JSONParser.getJSON(JSONParser.java:39)
10-27 02:52:44.724: E/AndroidRuntime(627): at com.example.rottentomatoesclient.MoviesActivity$getMovieList.doInBackground(MoviesActivity.java:90)
10-27 02:52:44.724: E/AndroidRuntime(627): at com.example.rottentomatoesclient.MoviesActivity$getMovieList.doInBackground(MoviesActivity.java:1)
10-27 02:52:44.724: E/AndroidRuntime(627): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-27 02:52:44.724: E/AndroidRuntime(627): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-27 02:52:44.724: E/AndroidRuntime(627): ... 5 more
10-27 02:52:44.774: W/ActivityManager(148): Force finishing activity com.example.rottentomatoesclient/.MoviesActivity

最佳答案

好的,您的 URL 可能正确。在长堆栈跟踪的中间,您可以找到这个:

Caused by: java.lang.IllegalStateException: 
Target host must not be null, or set in parameters. scheme=null, host=null,
path=api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json

这看起来像是从服务器返回的错误 - 您没有提供所有参数。

关于android - httpClient.execute(httpPost) 导致崩溃 (Android 4.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13097892/

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