0

AndroidプログラムのWebサービスからデータを取得する方法(以下)があります。Android2.2で正常に動作します。ただし、4.xでは、「ErrorRequestingAPI」というメッセージとともに例外がスローされます。私のコードでどの部分が例外を引き起こしているのか、そしてなぜ4.xでのみ例外が発生しているのかわからない誰かが私にいくつかのポインターを与えることができますか?

        public static synchronized String performHTTPRequest(HttpUriRequest request)
                throws ApiException, ParseException, IOException {
            HttpClientWrapper cli=new HttpClientWrapper();
            HttpClient client;
            try {
                client=cli.getClient();
                HttpResponse response = client.execute(request);
                // Check if server response is valid
                StatusLine status = response.getStatusLine() ;

            if (status.getStatusCode() != HTTP_STATUS_OK) {
                throw new ApiException("Invalid response from server: " + status.toString());
            }
            return EntityUtils.toString(response.getEntity());
        } catch (MalformedURLException e) {
            throw new Error(e);
        }  catch (Exception cnrce) {
            throw new ApiException("Error requesting API:");
        }
    }
4

1 に答える 1

1

4.0またはハニカムの上のバージョンでは、UIスレッドでhttpリクエストを実行できません。これらはasynctaskで実行する必要があります。編集: ドキュメント

asynctaskのコード例

于 2013-01-16T05:59:27.143 に答える