cz.msebera.android.httpclient.HttpResponse Java Examples

The following examples show how to use cz.msebera.android.httpclient.HttpResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NetWorkTool.java    From FoodOrdering with Apache License 2.0 6 votes vote down vote up
public static String getContent(String url) throws Exception {
    StringBuilder sb = new StringBuilder();

    HttpClient client = new DefaultHttpClient();
    HttpParams httpParams = (HttpParams) client.getParams();
    // 设置网络超时参数
    HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
    HttpConnectionParams.setSoTimeout(httpParams, 5000);
    HttpResponse response = client.execute(new HttpGet(url));
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                entity.getContent(), "GBK"), 8192);
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
    }
    return sb.toString();
}
 
Example #2
Source File: HttpWebConnection.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Converts an HttpMethod into a WebResponse.
 */
private WebResponse makeWebResponse(final HttpResponse httpResponse,
        final WebRequest request, final DownloadedContent responseBody, final long loadTime) {

    String statusMessage = httpResponse.getStatusLine().getReasonPhrase();
    if (statusMessage == null) {
        statusMessage = "Unknown status message";
    }
    final int statusCode = httpResponse.getStatusLine().getStatusCode();
    final List<NameValuePair> headers = new ArrayList<>();
    for (final Header header : httpResponse.getAllHeaders()) {
        headers.add(new NameValuePair(header.getName(), header.getValue()));
    }
    final WebResponseData responseData = new WebResponseData(responseBody, statusCode, statusMessage, headers);
    return newWebResponseInstance(responseData, loadTime, request);
}
 
Example #3
Source File: ProfileFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
@Override
protected String doInBackground(Void... params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        return EntityUtils.toString(httpEntity);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #4
Source File: HorochanModule.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String deletePost(DeletePostModel model, ProgressListener listener, CancellableTask task) throws Exception {
    String url = getUsingUrl(true) + "v1/posts/" + model.postNumber;
    HttpEntity entity = ExtendedMultipartBuilder.create().setDelegates(listener, task).addString("password", model.password).build();
    HttpUriRequest request = null;
    HttpResponse response = null;
    HttpEntity responseEntity = null;
    try {
        request = RequestBuilder.delete().setUri(url).setEntity(entity).build();
        response = httpClient.execute(request);
        StatusLine status = response.getStatusLine();
        switch (status.getStatusCode()) {
            case 200:
                return null;
            case 400:
                responseEntity = response.getEntity();
                InputStream stream = IOUtils.modifyInputStream(responseEntity.getContent(), null, task);
                JSONObject json = new JSONObject(new JSONTokener(new BufferedReader(new InputStreamReader(stream))));
                throw new Exception(json.getString("message"));
            default:
                throw new Exception(status.getStatusCode() + " - " + status.getReasonPhrase());
        }
    } finally {
        try { if (request != null) request.abort(); } catch (Exception e) {}
        EntityUtils.consumeQuietly(responseEntity);
        if (response != null && response instanceof Closeable) IOUtils.closeQuietly((Closeable) response);
    }
}
 
Example #5
Source File: HttpWebConnection.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Downloads the response body.
 * @param httpResponse the web server's response
 * @return a wrapper for the downloaded body.
 * @throws IOException in case of problem reading/saving the body
 */
protected DownloadedContent downloadResponseBody(final HttpResponse httpResponse) throws IOException {
    final HttpEntity httpEntity = httpResponse.getEntity();
    if (httpEntity == null) {
        return new DownloadedContent.InMemory(null);
    }

    try (InputStream is = httpEntity.getContent()) {
        return downloadContent(is, webClient_.getOptions().getMaxInMemory());
    }
}
 
Example #6
Source File: DownloadProfileImageActivity.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
@Override
protected String doInBackground(Void... params) {
    HttpClient httpClient = new DefaultHttpClient();
    try {
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        return EntityUtils.toString(httpEntity);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #7
Source File: IntroScreenActivity.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
@Override
protected String doInBackground(Void... params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(getResources().getString(R.string.get_user_info_url) + token);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        return EntityUtils.toString(httpEntity);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #8
Source File: MainActivity.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
@Override
protected String doInBackground(Void... params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(final_url);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        return EntityUtils.toString(httpEntity);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #9
Source File: MainActivity.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
@Override
protected String doInBackground(Void... params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        return EntityUtils.toString(httpEntity);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #10
Source File: OverviewDialog.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
@Override
protected String doInBackground(Void... params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        return EntityUtils.toString(httpEntity);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #11
Source File: FeedFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
@Override
protected String doInBackground(Void... params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        return EntityUtils.toString(httpEntity);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #12
Source File: HtmlUnitRedirectStrategie.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isRedirected(final HttpRequest request, final HttpResponse response,
        final HttpContext context) throws ProtocolException {
    return super.isRedirected(request, response, context) && response.getFirstHeader("location") != null;
}
 
Example #13
Source File: HttpClientWrapper.java    From Overchan-Android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
    return getClient().execute(request);
}
 
Example #14
Source File: HttpClientWrapper.java    From Overchan-Android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException {
    return getClient().execute(request, context);
}
 
Example #15
Source File: HttpClientWrapper.java    From Overchan-Android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public HttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException {
    return getClient().execute(target, request);
}
 
Example #16
Source File: HttpClientWrapper.java    From Overchan-Android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
    return getClient().execute(target, request, context);
}
 
Example #17
Source File: HttpUtils.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
public static String getResponseFromGetUrl(String url,
                                               String params) throws Exception {
        Log.d("Chen", "url--" + url);
        if (null != params && !"".equals(params)) {

            url = url + "?";

            String[] paramarray = params.split(",");

            for (int index = 0; null != paramarray && index < paramarray.length; index++) {

                if (index == 0) {

                    url = url + paramarray[index];
                } else {

                    url = url + "&" + paramarray[index];
                }

            }

        }

        HttpGet httpRequest = new HttpGet(url);

//        httpRequest.addHeader("Cookie", logininfo);

        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        int timeoutConnection = 30000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 30000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);

        // DefaultHttpClient httpclient = new DefaultHttpClient();
        StringBuffer sb = new StringBuffer();


        try {
            HttpResponse httpResponse = httpclient.execute(httpRequest);

            String inputLine = "";
            // Log.d("Chen","httpResponse.getStatusLine().getStatusCode()"+httpResponse.getStatusLine().getStatusCode());
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                InputStreamReader is = new InputStreamReader(httpResponse
                        .getEntity().getContent());
                BufferedReader in = new BufferedReader(is);
                while ((inputLine = in.readLine()) != null) {

                    sb.append(inputLine);
                }

                in.close();

            } else if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            httpclient.getConnectionManager().shutdown();
        }

        return sb.toString();

    }