当前位置: 首页>>代码示例>>Java>>正文


Java HttpResponse.getBodyAsString方法代码示例

本文整理汇总了Java中com.turbomanage.httpclient.HttpResponse.getBodyAsString方法的典型用法代码示例。如果您正苦于以下问题:Java HttpResponse.getBodyAsString方法的具体用法?Java HttpResponse.getBodyAsString怎么用?Java HttpResponse.getBodyAsString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.turbomanage.httpclient.HttpResponse的用法示例。


在下文中一共展示了HttpResponse.getBodyAsString方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: OnRun

import com.turbomanage.httpclient.HttpResponse; //导入方法依赖的package包/类
@Override
protected void OnRun() {
    String url = SignalAUtils.EnsureEndsWith(mConnection.getUrl(), "/");
	try {
		url += "abort?transport=LongPolling&connectionToken=" + URLEncoder.encode(mConnection.getConnectionToken(), "utf-8");
	} catch (UnsupportedEncodingException e) {
		Log.e(TAG, "Unsupported message encoding error, when encoding connectionToken.");
	}
	TransportHelper.AppendCustomQueryString(mConnection, url);

	AsyncCallback cb = new AsyncCallback() {
		
		@Override
		public void onComplete(HttpResponse httpResponse) {
			
			if(httpResponse.getStatus() != 200 || httpResponse.getBodyAsString() == null || httpResponse.getBodyAsString().isEmpty())
			{
				Log.e(TAG, "Clean disconnect failed. " + httpResponse.getStatus());
			}
			
			mConnection.SetNewState(new DisconnectedState(mConnection));
		}

        @Override
        public void onError(Exception ex) {
			mConnection.setError(ex);
			mConnection.SetNewState(new DisconnectedState(mConnection));
        }
	};

	ParallelHttpClient httpClient = new ParallelHttpClient();
	httpClient.setMaxRetries(1);
	ParameterMap params = httpClient.newParams();
	httpClient.post(url, params, cb);
}
 
开发者ID:yukozh,项目名称:CodeComb.Mobile.Android,代码行数:36,代码来源:DisconnectingState.java

示例2: fetchConferenceDataIfNewer

import com.turbomanage.httpclient.HttpResponse; //导入方法依赖的package包/类
/**
 * Fetches data from the remote server.
 *
 * @param refTimestamp The timestamp of the data to use as a reference; if the remote data
 *                     is not newer than this timestamp, no data will be downloaded and
 *                     this method will return null.
 *
 * @return The data downloaded, or null if there is no data to download
 * @throws IOException if an error occurred during download.
 */
public String[] fetchConferenceDataIfNewer(String refTimestamp) throws IOException {
    if (TextUtils.isEmpty(mManifestUrl)) {
        LOGW(TAG, "Manifest URL is empty (remote sync disabled!).");
        return null;
    }

    BasicHttpClient httpClient = new BasicHttpClient();
    httpClient.setRequestLogger(mQuietLogger);

    // Only download if data is newer than refTimestamp
    // Cloud Storage is very picky with the If-Modified-Since format. If it's in a wrong
    // format, it refuses to serve the file, returning 400 HTTP error. So, if the
    // refTimestamp is in a wrong format, we simply ignore it. But pay attention to this
    // warning in the log, because it might mean unnecessary data is being downloaded.
    if (!TextUtils.isEmpty(refTimestamp)) {
        if (TimeUtils.isValidFormatForIfModifiedSinceHeader(refTimestamp)) {
            httpClient.addHeader("If-Modified-Since", refTimestamp);
        } else {
            LOGW(TAG, "Could not set If-Modified-Since HTTP header. Potentially downloading " +
                    "unnecessary data. Invalid format of refTimestamp argument: "+refTimestamp);
        }
    }

    HttpResponse response = httpClient.get(mManifestUrl, null);
    if (response == null) {
        LOGE(TAG, "Request for manifest returned null response.");
        throw new IOException("Request for data manifest returned null response.");
    }

    int status = response.getStatus();
    if (status == HttpURLConnection.HTTP_OK) {
        LOGD(TAG, "Server returned HTTP_OK, so new data is available.");
        mServerTimestamp = getLastModified(response);
        LOGD(TAG, "Server timestamp for new data is: " + mServerTimestamp);
        String body = response.getBodyAsString();
        if (TextUtils.isEmpty(body)) {
            LOGE(TAG, "Request for manifest returned empty data.");
            throw new IOException("Error fetching conference data manifest: no data.");
        }
        LOGD(TAG, "Manifest "+mManifestUrl+" read, contents: " + body);
        mBytesDownloaded += body.getBytes().length;
        return processManifest(body);
    } else if (status == HttpURLConnection.HTTP_NOT_MODIFIED) {
        // data on the server is not newer than our data
        LOGD(TAG, "HTTP_NOT_MODIFIED: data has not changed since " + refTimestamp);
        return null;
    } else {
        LOGE(TAG, "Error fetching conference data: HTTP status " + status);
        throw new IOException("Error fetching conference data: HTTP status " + status);
    }
}
 
开发者ID:dreaminglion,项目名称:iosched-reader,代码行数:62,代码来源:RemoteConferenceDataFetcher.java

示例3: fetchFile

import com.turbomanage.httpclient.HttpResponse; //导入方法依赖的package包/类
/**
 * Fetches a file from the cache/network, from an absolute or relative URL. If the
 * file is available in our cache, we read it from there; if not, we will
 * download it from the network and cache it.
 *
 * @param url The URL to fetch the file from. The URL may be absolute or relative; if
 *            relative, it will be considered to be relative to the manifest URL.
 * @return The contents of the file.
 * @throws IOException If an error occurs.
 */
private String fetchFile(String url) throws IOException {
    // If this is a relative url, consider it relative to the manifest URL
    if (!url.contains("://")) {
        if (TextUtils.isEmpty(mManifestUrl) || !mManifestUrl.contains("/")) {
            LOGE(TAG, "Could not build relative URL based on manifest URL.");
            return null;
        }
        int i = mManifestUrl.lastIndexOf('/');
        url = mManifestUrl.substring(0, i) + "/" + url;
    }

    LOGD(TAG, "Attempting to fetch: " + sanitizeUrl(url));

    // Check if we have it in our cache first
    String body = null;
    try {
        body = loadFromCache(url);
        if (!TextUtils.isEmpty(body)) {
            // cache hit
            mBytesReadFromCache += body.getBytes().length;
            mCacheFilesToKeep.add(getCacheKey(url));
            return body;
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        LOGE(TAG, "IOException getting file from cache.");
        // proceed anyway to attempt to download it from the network
    }

    BasicHttpClient client = new BasicHttpClient();
    client.setRequestLogger(mQuietLogger);

    // We don't have the file on cache, so download it
    LOGD(TAG, "Cache miss. Downloading from network: " + sanitizeUrl(url));
    HttpResponse response = client.get(url, null);

    if (response == null) {
        throw new IOException("Request for URL " + sanitizeUrl(url) + " returned null response.");
    }

    LOGD(TAG, "HTTP response " + response.getStatus());
    if (response.getStatus() == HttpURLConnection.HTTP_OK) {
        body = response.getBodyAsString();
        if (TextUtils.isEmpty(body)) {
            throw new IOException("Got empty response when attempting to fetch " +
                    sanitizeUrl(url) + url);
        }
        LOGD(TAG, "Successfully downloaded from network: " + sanitizeUrl(url));
        mBytesDownloaded += body.getBytes().length;
        writeToCache(url, body);
        mCacheFilesToKeep.add(getCacheKey(url));
        return body;
    } else {
        LOGE(TAG, "Failed to fetch from network: " + sanitizeUrl(url));
        throw new IOException("Request for URL " + sanitizeUrl(url) +
                " failed with HTTP error " + response.getStatus());
    }
}
 
开发者ID:dreaminglion,项目名称:iosched-reader,代码行数:69,代码来源:RemoteConferenceDataFetcher.java

示例4: fetchFile

import com.turbomanage.httpclient.HttpResponse; //导入方法依赖的package包/类
/**
 * Fetches a file from the cache/network, from an absolute or relative URL. If the
 * file is available in our cache, we read it from there; if not, we will
 * download it from the network and cache it.
 *
 * @param url The URL to fetch the file from. The URL may be absolute or relative; if
 *            relative, it will be considered to be relative to the manifest URL.
 * @return The contents of the file.
 * @throws IOException If an error occurs.
 */
private String fetchFile(String url) throws IOException {
    // If this is a relative url, consider it relative to the manifest URL
    if (!url.contains("://")) {
        if (TextUtils.isEmpty(mManifestUrl) || !mManifestUrl.contains("/")) {
            LOGE(TAG, "Could not build relative URL based on manifest URL.");
            return null;
        }
        int i = mManifestUrl.lastIndexOf('/');
        url = mManifestUrl.substring(0, i) + "/" + url;
    }

    LOGD(TAG, "Attempting to fetch: " + sanitizeUrl(url));

    // Check if we have it in our cache first
    String body = null;
    try {
        body = loadFromCache(url);
        if (!TextUtils.isEmpty(body)) {
            // cache hit
            mBytesReadFromCache += body.getBytes().length;
            mCacheFilesToKeep.add(getCacheKey(url));
            return body;
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        LOGE(TAG, "IOException getting file from cache.");
        // proceed anyway to attempt to download it from the network
    }

    // We don't have the file on cache, so download it
    LOGD(TAG, "Cache miss. Downloading from network: " + sanitizeUrl(url));
    BasicHttpClient client = new BasicHttpClient();
    client.setRequestLogger(mQuietLogger);
    HttpResponse response = client.get(url, null);

    if (response == null) {
        throw new IOException("Request for URL " + sanitizeUrl(url) + " returned null response.");
    }

    LOGD(TAG, "HTTP response " + response.getStatus());
    if (response.getStatus() == HttpURLConnection.HTTP_OK) {
        body = response.getBodyAsString();
        if (TextUtils.isEmpty(body)) {
            throw new IOException("Got empty response when attempting to fetch " +
                    sanitizeUrl(url));
        }
        LOGD(TAG, "Successfully downloaded from network: " + sanitizeUrl(url));
        mBytesDownloaded += body.getBytes().length;
        writeToCache(url, body);
        mCacheFilesToKeep.add(getCacheKey(url));
        return body;
    } else {
        LOGE(TAG, "Failed to fetch from network: " + sanitizeUrl(url));
        throw new IOException("Request for URL " + sanitizeUrl(url) +
                " failed with HTTP error " + response.getStatus());
    }
}
 
开发者ID:The-WebOps-Club,项目名称:saarang-iosched,代码行数:68,代码来源:RemoteConferenceDataFetcher.java

示例5: fetchConferenceDataIfNewer

import com.turbomanage.httpclient.HttpResponse; //导入方法依赖的package包/类
/**
 * Fetches data from the remote server.
 *
 * @param refTimestamp The timestamp of the data to use as a reference; if the remote data is
 *                     not newer than this timestamp, no data will be downloaded and this method
 *                     will return null.
 * @return The data downloaded, or null if there is no data to download
 * @throws IOException if an error occurred during download.
 */
public String[] fetchConferenceDataIfNewer(String refTimestamp) throws IOException {
    if (TextUtils.isEmpty(mManifestUrl)) {
        LOGW(TAG, "Manifest URL is empty (remote sync disabled!).");
        return null;
    }

    BasicHttpClient httpClient = new BasicHttpClient();
    httpClient.setRequestLogger(mQuietLogger);

    IOUtils.authorizeHttpClient(mContext, httpClient);

    // Only download if data is newer than refTimestamp
    // Cloud Storage is very picky with the If-Modified-Since format. If it's in a wrong
    // format, it refuses to serve the file, returning 400 HTTP error. So, if the
    // refTimestamp is in a wrong format, we simply ignore it. But pay attention to this
    // warning in the log, because it might mean unnecessary data is being downloaded.
    if (!TextUtils.isEmpty(refTimestamp)) {
        if (TimeUtils.isValidFormatForIfModifiedSinceHeader(refTimestamp)) {
            httpClient.addHeader("If-Modified-Since", refTimestamp);
        } else {
            LOGW(TAG, "Could not set If-Modified-Since HTTP header. Potentially downloading " +
                    "unnecessary data. Invalid format of refTimestamp argument: " +
                    refTimestamp);
        }
    }

    HttpResponse response = httpClient.get(mManifestUrl, null);
    if (response == null) {
        LOGE(TAG, "Request for manifest returned null response.");
        throw new IOException("Request for data manifest returned null response.");
    }

    int status = response.getStatus();
    if (status == HttpURLConnection.HTTP_OK) {
        LOGD(TAG, "Server returned HTTP_OK, so new data is available.");
        mServerTimestamp = getLastModified(response);
        LOGD(TAG, "Server timestamp for new data is: " + mServerTimestamp);
        String body = response.getBodyAsString();
        if (TextUtils.isEmpty(body)) {
            LOGE(TAG, "Request for manifest returned empty data.");
            throw new IOException("Error fetching conference data manifest: no data.");
        }
        LOGD(TAG, "Manifest " + mManifestUrl + " read, contents: " + body);
        mBytesDownloaded += body.getBytes().length;
        return processManifest(body);
    } else if (status == HttpURLConnection.HTTP_NOT_MODIFIED) {
        // data on the server is not newer than our data
        LOGD(TAG, "HTTP_NOT_MODIFIED: data has not changed since " + refTimestamp);
        return null;
    } else {
        LOGE(TAG, "Error fetching conference data: HTTP status " + status + " and manifest " +
                mManifestUrl);
        throw new IOException("Error fetching conference data: HTTP status " + status);
    }
}
 
开发者ID:google,项目名称:iosched,代码行数:65,代码来源:RemoteConferenceDataFetcher.java

示例6: fetchFile

import com.turbomanage.httpclient.HttpResponse; //导入方法依赖的package包/类
/**
 * Fetches a file from the cache/network, from an absolute or relative URL. If the file is
 * available in our cache, we read it from there; if not, we will download it from the network
 * and cache it.
 *
 * @param url The URL to fetch the file from. The URL may be absolute or relative; if relative,
 *            it will be considered to be relative to the manifest URL.
 * @return The contents of the file.
 * @throws IOException If an error occurs.
 */
private String fetchFile(String url) throws IOException {
    // If this is a relative url, consider it relative to the manifest URL
    if (!url.contains("://")) {
        if (TextUtils.isEmpty(mManifestUrl) || !mManifestUrl.contains("/")) {
            LOGE(TAG, "Could not build relative URL based on manifest URL.");
            return null;
        }
        int i = mManifestUrl.lastIndexOf('/');
        url = mManifestUrl.substring(0, i) + "/" + url;
    }

    LOGD(TAG, "Attempting to fetch: " + sanitizeUrl(url));

    // Check if we have it in our cache first
    String body;
    try {
        body = loadFromCache(url);
        if (!TextUtils.isEmpty(body)) {
            // cache hit
            mBytesReadFromCache += body.getBytes().length;
            mCacheFilesToKeep.add(getCacheKey(url));
            return body;
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        LOGE(TAG, "IOException getting file from cache.");
        // proceed anyway to attempt to download it from the network
    }

    BasicHttpClient client = new BasicHttpClient();
    IOUtils.authorizeHttpClient(mContext, client);
    client.setRequestLogger(mQuietLogger);

    // We don't have the file on cache, so download it
    LOGD(TAG, "Cache miss. Downloading from network: " + sanitizeUrl(url));
    HttpResponse response = client.get(url, null);

    if (response == null) {
        throw new IOException(
                "Request for URL " + sanitizeUrl(url) + " returned null response.");
    }

    LOGD(TAG, "HTTP response " + response.getStatus());
    if (response.getStatus() == HttpURLConnection.HTTP_OK) {
        body = response.getBodyAsString();
        if (TextUtils.isEmpty(body)) {
            throw new IOException("Got empty response when attempting to fetch " +
                    sanitizeUrl(url) + url);
        }
        LOGD(TAG, "Successfully downloaded from network: " + sanitizeUrl(url));
        mBytesDownloaded += body.getBytes().length;
        writeToCache(url, body);
        mCacheFilesToKeep.add(getCacheKey(url));
        return body;
    } else {
        LOGE(TAG, "Failed to fetch from network: " + sanitizeUrl(url));
        throw new IOException("Request for URL " + sanitizeUrl(url) +
                " failed with HTTP error " + response.getStatus());
    }
}
 
开发者ID:google,项目名称:iosched,代码行数:71,代码来源:RemoteConferenceDataFetcher.java

示例7: fetchConferenceDataIfNewer

import com.turbomanage.httpclient.HttpResponse; //导入方法依赖的package包/类
/**
 * Fetches data from the remote server.
 *
 * @param refTimestamp The timestamp of the data to use as a reference; if the remote data
 *                     is not newer than this timestamp, no data will be downloaded and
 *                     this method will return null.
 *
 * @return The data downloaded, or null if there is no data to download
 * @throws java.io.IOException if an error occurred during download.
 */
public String[] fetchConferenceDataIfNewer(String refTimestamp) throws IOException {
    if (TextUtils.isEmpty(mManifestUrl)) {
        LOGW(TAG, "Manifest URL is empty (remote sync disabled!).");
        return null;
    }

    BasicHttpClient httpClient = new BasicHttpClient();
    httpClient.setRequestLogger(mQuietLogger);

    // Only download if data is newer than refTimestamp
    // Cloud Storage is very picky with the If-Modified-Since format. If it's in a wrong
    // format, it refuses to serve the file, returning 400 HTTP error. So, if the
    // refTimestamp is in a wrong format, we simply ignore it. But pay attention to this
    // warning in the log, because it might mean unnecessary data is being downloaded.
    if (!TextUtils.isEmpty(refTimestamp)) {
        if (TimeUtils.isValidFormatForIfModifiedSinceHeader(refTimestamp)) {
            httpClient.addHeader("If-Modified-Since", refTimestamp);
        } else {
            LOGW(TAG, "Could not set If-Modified-Since HTTP header. Potentially downloading " +
                    "unnecessary data. Invalid format of refTimestamp argument: "+refTimestamp);
        }
    }

    HttpResponse response = httpClient.get(mManifestUrl, null);
    if (response == null) {
        LOGE(TAG, "Request for manifest returned null response.");
        throw new IOException("Request for data manifest returned null response.");
    }

    int status = response.getStatus();
    if (status == HttpURLConnection.HTTP_OK) {
        LOGD(TAG, "Server returned HTTP_OK, so new data is available.");
        mServerTimestamp = getLastModified(response);
        LOGD(TAG, "Server timestamp for new data is: " + mServerTimestamp);
        String body = response.getBodyAsString();
        if (TextUtils.isEmpty(body)) {
            LOGE(TAG, "Request for manifest returned empty data.");
            throw new IOException("Error fetching conference data manifest: no data.");
        }
        LOGD(TAG, "Manifest "+mManifestUrl+" read, contents: " + body);
        mBytesDownloaded += body.getBytes().length;
        return processManifest(body);
    } else if (status == HttpURLConnection.HTTP_NOT_MODIFIED) {
        // data on the server is not newer than our data
        LOGD(TAG, "HTTP_NOT_MODIFIED: data has not changed since " + refTimestamp);
        return null;
    } else {
        LOGE(TAG, "Error fetching conference data: HTTP status " + status);
        throw new IOException("Error fetching conference data: HTTP status " + status);
    }
}
 
开发者ID:ramonrabello,项目名称:devfestnorte-app,代码行数:62,代码来源:RemoteConferenceDataFetcher.java

示例8: fetchFile

import com.turbomanage.httpclient.HttpResponse; //导入方法依赖的package包/类
/**
 * Fetches a file from the cache/network, from an absolute or relative URL. If the
 * file is available in our cache, we read it from there; if not, we will
 * download it from the network and cache it.
 *
 * @param url The URL to fetch the file from. The URL may be absolute or relative; if
 *            relative, it will be considered to be relative to the manifest URL.
 * @return The contents of the file.
 * @throws java.io.IOException If an error occurs.
 */
private String fetchFile(String url) throws IOException {
    // If this is a relative url, consider it relative to the manifest URL
    if (!url.contains("://")) {
        if (TextUtils.isEmpty(mManifestUrl) || !mManifestUrl.contains("/")) {
            LOGE(TAG, "Could not build relative URL based on manifest URL.");
            return null;
        }
        int i = mManifestUrl.lastIndexOf('/');
        url = mManifestUrl.substring(0, i) + "/" + url;
    }

    LOGD(TAG, "Attempting to fetch: " + sanitizeUrl(url));

    // Check if we have it in our cache first
    String body = null;
    try {
        body = loadFromCache(url);
        if (!TextUtils.isEmpty(body)) {
            // cache hit
            mBytesReadFromCache += body.getBytes().length;
            mCacheFilesToKeep.add(getCacheKey(url));
            return body;
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        LOGE(TAG, "IOException getting file from cache.");
        // proceed anyway to attempt to download it from the network
    }

    // We don't have the file on cache, so download it
    LOGD(TAG, "Cache miss. Downloading from network: " + sanitizeUrl(url));
    BasicHttpClient client = new BasicHttpClient();
    client.setRequestLogger(mQuietLogger);
    HttpResponse response = client.get(url, null);

    if (response == null) {
        throw new IOException("Request for URL " + sanitizeUrl(url) + " returned null response.");
    }

    LOGD(TAG, "HTTP response " + response.getStatus());
    if (response.getStatus() == HttpURLConnection.HTTP_OK) {
        body = response.getBodyAsString();
        if (TextUtils.isEmpty(body)) {
            throw new IOException("Got empty response when attempting to fetch " +
                    sanitizeUrl(url));
        }
        LOGD(TAG, "Successfully downloaded from network: " + sanitizeUrl(url));
        mBytesDownloaded += body.getBytes().length;
        writeToCache(url, body);
        mCacheFilesToKeep.add(getCacheKey(url));
        return body;
    } else {
        LOGE(TAG, "Failed to fetch from network: " + sanitizeUrl(url));
        throw new IOException("Request for URL " + sanitizeUrl(url) +
                " failed with HTTP error " + response.getStatus());
    }
}
 
开发者ID:ramonrabello,项目名称:devfestnorte-app,代码行数:68,代码来源:RemoteConferenceDataFetcher.java


注:本文中的com.turbomanage.httpclient.HttpResponse.getBodyAsString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。