Search in sources :

Example 61 with HttpEntity

use of org.apache.http.HttpEntity in project musicbrainz-android by jdamcd.

the class MusicBrainzWebClient method searchArtist.

@Override
public LinkedList<ArtistSearchResult> searchArtist(String searchTerm) throws IOException {
    HttpEntity entity = get(QueryBuilder.artistSearch(searchTerm));
    LinkedList<ArtistSearchResult> artists = responseParser.parseArtistSearch(entity.getContent());
    entity.consumeContent();
    return artists;
}
Also used : HttpEntity(org.apache.http.HttpEntity) ArtistSearchResult(org.musicbrainz.android.api.data.ArtistSearchResult)

Example 62 with HttpEntity

use of org.apache.http.HttpEntity in project musicbrainz-android by jdamcd.

the class MusicBrainzWebClient method lookupRecording.

@Override
public Recording lookupRecording(String mbid) throws IOException {
    HttpEntity entity = get(QueryBuilder.recordingLookup(mbid));
    Recording recording = responseParser.parseRecording(entity.getContent());
    entity.consumeContent();
    return recording;
}
Also used : HttpEntity(org.apache.http.HttpEntity) Recording(org.musicbrainz.android.api.data.Recording)

Example 63 with HttpEntity

use of org.apache.http.HttpEntity in project musicbrainz-android by jdamcd.

the class MusicBrainzWebClient method searchRelease.

@Override
public LinkedList<ReleaseInfo> searchRelease(String searchTerm) throws IOException {
    HttpEntity entity = get(QueryBuilder.releaseSearch(searchTerm));
    LinkedList<ReleaseInfo> releases = responseParser.parseReleaseSearch(entity.getContent());
    entity.consumeContent();
    return releases;
}
Also used : HttpEntity(org.apache.http.HttpEntity) ReleaseInfo(org.musicbrainz.android.api.data.ReleaseInfo)

Example 64 with HttpEntity

use of org.apache.http.HttpEntity in project SmartAndroidSource by jaychou2012.

the class HttpClientStack method setEntityIfNonEmptyBody.

private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
        HttpEntity entity = new ByteArrayEntity(body);
        httpRequest.setEntity(entity);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Example 65 with HttpEntity

use of org.apache.http.HttpEntity in project FastDev4Android by jiangqqlmj.

the class IoUtils method responseFromServiceByGet.

/**
     * get请求获取服务端数据
     */
public static String responseFromServiceByGet(String url, HashMap<String, String> map, RequestCallBack requestCallBack) {
    if (url == null || url.equals("")) {
        return null;
    }
    if (requestCallBack != null) {
        requestCallBack.onRequestStart();
    }
    if (map != null) {
        StringBuilder sb = new StringBuilder(url);
        sb.append('?');
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey().toString();
            String value = null;
            if (entry.getValue() == null) {
                value = "";
            } else {
                value = entry.getValue().toString();
            }
            sb.append(key);
            sb.append('=');
            try {
                Log.d(TAG_LISTLOGIC, "get获取数据的key:" + key);
                Log.d(TAG_LISTLOGIC, "get获取数据的value:" + value);
                value = URLEncoder.encode(value, HTTP.UTF_8);
                Log.d(TAG_LISTLOGIC, "get投递编码后value:" + value);
                sb.append(value);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            sb.append('&');
        }
        // 删除最后一个"&"
        sb.deleteCharAt(sb.length() - 1);
        url = sb.toString();
    }
    Log.d(TAG_LISTLOGIC, "get请求地址" + url);
    if (requestCallBack != null) {
        requestCallBack.onRequestLoading();
    }
    HttpGet httpGet = null;
    URI encodedUri = null;
    InputStream is = null;
    try {
        encodedUri = new URI(url);
        httpGet = new HttpGet(encodedUri);
    } catch (URISyntaxException e) {
        // 清理一些空格
        String encodedUrl = url.replace(' ', '+');
        httpGet = new HttpGet(encodedUrl);
        e.printStackTrace();
    }
    HttpClient httpClient = new DefaultHttpClient();
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEF_CONN_TIMEOUT);
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, DEF_SOCKET_TIMEOUT);
    try {
        HttpResponse httpResponse = httpClient.execute(httpGet);
        if (httpResponse != null) {
            int httpCode = httpResponse.getStatusLine().getStatusCode();
            if (httpCode == HttpStatus.SC_OK) {
                HttpEntity entity = httpResponse.getEntity();
                Header header = httpResponse.getFirstHeader("Content-Encoding");
                if (header != null && header.getValue().equals("gzip")) {
                    Log.d(TAG_LISTLOGIC, "数据已做gzip压缩...");
                    // gzip压缩
                    byte[] resultstream = EntityUtils.toByteArray(entity);
                    resultstream = unGZip(resultstream);
                    String resultString = new String(resultstream, "UTF-8");
                    if (requestCallBack != null) {
                        requestCallBack.onRequestSuccess(resultString);
                    }
                    return resultString;
                } else {
                    Log.d(TAG_LISTLOGIC, "数据无Gzip压缩...");
                    // 无压缩
                    is = entity.getContent();
                    if (is != null) {
                        String resultString = new String(getByteArrayFromInputstream(is), "utf-8");
                        if (requestCallBack != null) {
                            requestCallBack.onRequestSuccess(resultString);
                        }
                        return resultString;
                    }
                }
                Log.d(TAG_LISTLOGIC, "get请求服务器返回值200");
            } else {
                httpGet.abort();
                if (requestCallBack != null) {
                    requestCallBack.onRequestError(RequestCallBack.HTTPSTATUSERROR, "HTTP链接错误");
                }
            }
        } else {
            httpGet.abort();
            if (requestCallBack != null) {
                requestCallBack.onRequestError(RequestCallBack.HTTPRESPONSEERROR, "数据获取异常");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        if (requestCallBack != null) {
            requestCallBack.onRequestError(RequestCallBack.HTTPRESPONSEERROR, "数据获取异常");
        }
        return null;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        if (requestCallBack != null) {
            requestCallBack.onRequestError(RequestCallBack.OUTOFMEMORYERROR, "内存溢出");
        }
        return null;
    } finally {
        if (httpClient != null) {
            httpClient.getConnectionManager().shutdown();
            httpClient = null;
        }
        if (requestCallBack != null) {
            requestCallBack.onCancel();
        }
    }
    return null;
}
Also used : HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) DataInputStream(java.io.DataInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpResponse(org.apache.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Header(org.apache.http.Header) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HttpEntity (org.apache.http.HttpEntity)518 HttpResponse (org.apache.http.HttpResponse)185 HttpGet (org.apache.http.client.methods.HttpGet)152 IOException (java.io.IOException)144 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)105 Test (org.junit.Test)93 HttpPost (org.apache.http.client.methods.HttpPost)84 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)83 InputStream (java.io.InputStream)71 ArrayList (java.util.ArrayList)69 Header (org.apache.http.Header)64 StatusLine (org.apache.http.StatusLine)61 URI (java.net.URI)58 NameValuePair (org.apache.http.NameValuePair)58 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)58 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)58 HttpClient (org.apache.http.client.HttpClient)55 StringEntity (org.apache.http.entity.StringEntity)51 InputStreamReader (java.io.InputStreamReader)44 BufferedReader (java.io.BufferedReader)41