• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java BasicHttpClient类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中com.turbomanage.httpclient.BasicHttpClient的典型用法代码示例。如果您正苦于以下问题:Java BasicHttpClient类的具体用法?Java BasicHttpClient怎么用?Java BasicHttpClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



BasicHttpClient类属于com.turbomanage.httpclient包,在下文中一共展示了BasicHttpClient类的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: getUserInfo

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的package包/类
private User getUserInfo(FBUser fbUser) {
    User newUser = new User();
    newUser.setEmailAddress(fbUser.email);
    newUser.setFirstName(fbUser.first_name);
    newUser.setLastName(fbUser.last_name);
    newUser.setFacebookId(fbUser.id);
    BasicHttpClient httpClient = new BasicHttpClient();
    String url = "https://graph.facebook.com/" + fbUser.id + "/picture?redirect=0&type=small";
    String picData = httpClient.get(url, null).getBodyAsString();
    ObjectMapper om = new ObjectMapper();
    try {
        String imgUrl = om.readValue(picData, PicData.class).data.url;
        newUser.setImgUrl(imgUrl);
    } catch (IOException e) {
        new RestException(e);
    }
    return newUser;
}
 
开发者ID:turbomanage,项目名称:listmaker,代码行数:19,代码来源:Facebook.java


示例2: SyncHelper

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的package包/类
/**
 *
 * @param context Can be Application, Activity or Service context.
 */
public SyncHelper(Context context) {
    mContext = context;
    mConferenceDataHandler = new ConferenceDataHandler(mContext);
    mRemoteDataFetcher = new RemoteConferenceDataFetcher(mContext);
    mHttpClient = new BasicHttpClient();
}
 
开发者ID:dreaminglion,项目名称:iosched-reader,代码行数:11,代码来源:SyncHelper.java


示例3: sendSessionToServer

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的package包/类
/**
 * Posts a session to the event server.
 *
 * @param sessionId The ID of the session that was reviewed.
 * @return whether or not updating succeeded
 */
public boolean sendSessionToServer(String sessionId, List<String> questions) {

    BasicHttpClient httpClient = new BasicHttpClient();
    httpClient.addHeader(PARAMETER_EVENT_CODE, Config.FEEDBACK_API_CODE);
    httpClient.addHeader(PARAMETER_API_KEY, Config.FEEDBACK_API_KEY);

    ParameterMap parameterMap = httpClient.newParams();
    parameterMap.add(PARAMETER_SESSION_ID, sessionId);
    parameterMap.add(PARAMETER_SURVEY_ID, Config.FEEDBACK_SURVEY_ID);
    parameterMap.add(PARAMETER_REGISTRANT_ID, Config.FEEDBACK_DUMMY_REGISTRANT_ID);
    int i = 1;
    for (String question : questions) {
        parameterMap.add("q" + i, question);
        i++;
    }

    HttpResponse response = httpClient.get(mUrl, parameterMap);

    if (response != null && response.getStatus() == HttpURLConnection.HTTP_OK) {
        LogUtils.LOGD(TAG, "Server returned HTTP_OK, so session posting was successful.");
        return true;
    } else {
        LogUtils.LOGE(TAG, "Error posting session: HTTP status " + response.getStatus());
        return false;
    }
}
 
开发者ID:The-WebOps-Club,项目名称:saarang-iosched,代码行数:33,代码来源:EventFeedbackApi.java


示例4: sendSessionToServer

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的package包/类
/**
 * Posts a session to the event server.
 *
 * @param sessionId The ID of the session that was reviewed.
 * @return whether or not updating succeeded
 */
public boolean sendSessionToServer(String sessionId, List<String> questions) {

    BasicHttpClient httpClient = new BasicHttpClient();
    httpClient.addHeader(PARAMETER_EVENT_CODE, Config.FEEDBACK_API_CODE);
    httpClient.addHeader(PARAMETER_API_KEY, Config.FEEDBACK_API_KEY);

    ParameterMap parameterMap = httpClient.newParams();
    parameterMap.add(PARAMETER_SESSION_ID, sessionId);
    parameterMap.add(PARAMETER_SURVEY_ID, Config.FEEDBACK_SURVEY_ID);
    parameterMap.add(PARAMETER_REGISTRANT_ID, Config.FEEDBACK_DUMMY_REGISTRANT_ID);
    int i = 1;
    for (String question : questions) {
        parameterMap.add("q" + i, question);
        i++;
    }

    HttpResponse response = httpClient.get(mUrl, parameterMap);

    if (response != null && response.getStatus() == HttpURLConnection.HTTP_OK) {
        LOGD(TAG, "Server returned HTTP_OK, so session posting was successful.");
        return true;
    } else {
        LOGE(TAG, "Error posting session: HTTP status " + response.getStatus());
        return false;
    }
}
 
开发者ID:gdg-bh,项目名称:AppDevFestSudeste2015,代码行数:33,代码来源:EventFeedbackApi.java


示例5: authorizeHttpClient

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的package包/类
/**
 * If {@code AUTHORIZATION_TO_BACKEND_REQUIRED} is true add an authentication header to the
 * given request. The currently signed in user is used to retrieve the auth token.
 * Allows pre-release builds to use a Google Cloud storage bucket with non-public data.
 *
 * @param context Context used to retrieve auth token from SharedPreferences.
 * @param basicHttpClient HTTP client to which the authorization header will be added.
 */
public static void authorizeHttpClient(Context context, BasicHttpClient basicHttpClient) {
    if (basicHttpClient == null || AccountUtils.getAuthToken(context) == null) {
        return;
    }
    if (AUTHORIZATION_TO_BACKEND_REQUIRED) {
        basicHttpClient.addHeader(AUTHORIZATION_HEADER,
                BEARER_PREFIX + AccountUtils.getAuthToken(context));
    }
}
 
开发者ID:google,项目名称:iosched,代码行数:18,代码来源:IOUtils.java


示例6: SyncHelper

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的package包/类
/**
 *
 * @param context Can be Application, Activity or Service context.
 */
public SyncHelper(Context context) {
    mContext = context;
    mConferenceDataHandler = new ConferenceDataHandler(mContext);
    mRemoteDataFetcher = new RemoteConferenceDataFetcher(mContext);
    mHttpClient = new BasicHttpClient();
    if (!BuildConfig.DEBUG) {
        mHttpClient.setRequestLogger(new MinimalRequestLogger());
    }
}
 
开发者ID:google,项目名称:iosched,代码行数:14,代码来源:SyncHelper.java


示例7: remoteSyncMapData

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的package包/类
private ArrayList<ContentProviderOperation> remoteSyncMapData(String urlString,
        SharedPreferences preferences) throws IOException {
    final String localVersion = preferences.getString("local_mapdata_version", null);

    ArrayList<ContentProviderOperation> batch = Lists.newArrayList();

    BasicHttpClient httpClient = new BasicHttpClient();
    httpClient.setRequestLogger(mQuietLogger);
    httpClient.addHeader("If-None-Match", localVersion);

    LOGD(TAG,"Local map version: "+localVersion);
    HttpResponse response = httpClient.get(urlString, null);
    final int status = response.getStatus();

    if (status == HttpURLConnection.HTTP_OK) {
        // Data has been updated, otherwise would have received HTTP_NOT_MODIFIED
        LOGI(TAG, "Remote syncing map data");
        final List<String> etag = response.getHeaders().get("ETag");
        if (etag != null && etag.size() > 0) {
            MapPropertyHandler handler = new MapPropertyHandler(mContext);
            batch.addAll(handler.parse(response.getBodyAsString()));
            syncMapTiles(handler.getTiles());

            // save new etag as version
            preferences.edit().putString("local_mapdata_version", etag.get(0)).commit();
        }
    } //else: no update

    return batch;
}
 
开发者ID:TheDeltaProgram,项目名称:iosched2013,代码行数:31,代码来源:SyncHelper.java


示例8: syncMapTiles

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的package包/类
/**
 * Synchronise the map overlay files either from the local assets (if available) or from a remote url.
 *
 * @param collection Set of tiles containing a local filename and remote url.
 * @throws IOException
 */
private void syncMapTiles(Collection<Tile> collection) throws IOException, SVGParseException {

    //keep track of used files, unused files are removed
    ArrayList<String> usedTiles = Lists.newArrayList();
    for(Tile tile : collection){
        final String filename = tile.filename;
        final String url = tile.url;

        usedTiles.add(filename);

        if (!MapUtils.hasTile(mContext, filename)) {
            // copy or download the tile if it is not stored yet
            if (MapUtils.hasTileAsset(mContext, filename)) {
                // file already exists as an asset, copy it
                MapUtils.copyTileAsset(mContext, filename);
            } else {
                // download the file
                File tileFile = MapUtils.getTileFile(mContext, filename);
                BasicHttpClient httpClient = new BasicHttpClient();
                httpClient.setRequestLogger(mQuietLogger);
                HttpResponse httpResponse = httpClient.get(url, null);
                writeFile(httpResponse.getBody(), tileFile);

                // ensure the file is valid SVG
                InputStream is = new FileInputStream(tileFile);
                SVG svg = SVGParser.getSVGFromInputStream(is);
                is.close();
            }
        }
    }

    MapUtils.removeUnusedTiles(mContext, usedTiles);
}
 
开发者ID:TheDeltaProgram,项目名称:iosched2013,代码行数:40,代码来源:SyncHelper.java


示例9: FeedbackApiHelper

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的package包/类
public FeedbackApiHelper(BasicHttpClient httpClient, String url) {
    mHttpClient = httpClient;
    mUrl = url;
}
 
开发者ID:dreaminglion,项目名称:iosched-reader,代码行数:5,代码来源:FeedbackApiHelper.java


示例10: fetchConferenceDataIfNewer

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的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


示例11: fetchFile

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的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


示例12: fetchFile

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的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


示例13: fetchConferenceDataIfNewer

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的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


示例14: fetchFile

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的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


示例15: fetchConferenceDataIfNewer

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的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


示例16: fetchFile

import com.turbomanage.httpclient.BasicHttpClient; //导入依赖的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.BasicHttpClient类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java OneForOneStrategy类代码示例发布时间:2022-05-21
下一篇:
Java CANTimeoutException类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap