Java Code Examples for com.android.volley.VolleyLog#d()

The following examples show how to use com.android.volley.VolleyLog#d() . 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: DiskBasedCache.java    From volley_demo with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the contents of this CacheHeader to the specified OutputStream.
 */
public boolean writeHeader(OutputStream os) {
    try {
        writeInt(os, CACHE_MAGIC);
        writeString(os, key);
        writeString(os, etag == null ? "" : etag);
        writeLong(os, serverDate);
        writeLong(os, lastModified);
        writeLong(os, ttl);
        writeLong(os, softTtl);
        writeStringStringMap(responseHeaders, os);
        os.flush();
        return true;
    } catch (IOException e) {
        VolleyLog.d("%s", e.toString());
        return false;
    }
}
 
Example 2
Source File: DiskBasedCache.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the contents of this CacheHeader to the specified OutputStream.
 */
public boolean writeHeader(OutputStream os) {
    try {
        writeInt(os, CACHE_MAGIC);
        writeString(os, key);
        writeString(os, etag == null ? "" : etag);
        writeLong(os, serverDate);
        writeLong(os, ttl);
        writeLong(os, softTtl);
        writeStringStringMap(responseHeaders, os);
        os.flush();
        return true;
    } catch (IOException e) {
        VolleyLog.d("%s", e.toString());
        return false;
    }
}
 
Example 3
Source File: ColumnUtils.java    From volley with Apache License 2.0 6 votes vote down vote up
public static Method getColumnGetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    Method getMethod = null;
    if (field.getType() == boolean.class) {
        getMethod = getBooleanColumnGetMethod(entityType, fieldName);
    }
    if (getMethod == null) {
        String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
            getMethod = entityType.getDeclaredMethod(methodName);
        } catch (NoSuchMethodException e) {
            VolleyLog.d(methodName + " not exist");
        }
    }

    if (getMethod == null && !Object.class.equals(entityType.getSuperclass())) {
        return getColumnGetMethod(entityType.getSuperclass(), field);
    }
    return getMethod;
}
 
Example 4
Source File: DiskBasedCache.java    From jus with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the contents of this CacheHeader to the specified OutputStream.
 */
public boolean writeHeader(OutputStream os) {
    try {
        writeInt(os, CACHE_MAGIC);
        writeString(os, key);
        writeString(os, etag == null ? "" : etag);
        writeLong(os, serverDate);
        writeLong(os, lastModified);
        writeLong(os, ttl);
        writeLong(os, softTtl);
        writeStringStringMap(responseHeaders, os);
        os.flush();
        return true;
    } catch (IOException e) {
        VolleyLog.d("%s", e.toString());
        return false;
    }
}
 
Example 5
Source File: DiskBasedCache.java    From android-common-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Clears the cache. Deletes all cached files from disk.
 */
@Override
public synchronized void clear() {
    File[] files = mRootDirectory.listFiles();
    if (files != null) {
        for (File file : files) {
            file.delete();
        }
    }
    mEntries.clear();
    mTotalSize = 0;
    VolleyLog.d("Cache cleared.");
}
 
Example 6
Source File: DiskBasedCache.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the cache entry with the specified key if it exists, null otherwise.
 */
@Override
public synchronized Entry get(String key) {
    CacheHeader entry = mEntries.get(key);
    // if the entry does not exist, return.
    if (entry == null) {
        return null;
    }

    File file = getFileForKey(key);
    CountingInputStream cis = null;
    try {
        cis = new CountingInputStream(new BufferedInputStream(new FileInputStream(file)));
        CacheHeader.readHeader(cis); // eat header
        byte[] data = streamToBytes(cis, (int) (file.length() - cis.bytesRead));
        return entry.toCacheEntry(data);
    } catch (IOException e) {
        VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString());
        remove(key);
        return null;
    } finally {
        if (cis != null) {
            try {
                cis.close();
            } catch (IOException ioe) {
                return null;
            }
        }
    }
}
 
Example 7
Source File: BasicNetwork.java    From jus with Apache License 2.0 5 votes vote down vote up
/**
 * Logs requests that took over SLOW_REQUEST_THRESHOLD_MS to complete.
 */
private void logSlowRequests(long requestLifetime, Request<?> request,
        byte[] responseContents, StatusLine statusLine) {
    if (DEBUG || requestLifetime > SLOW_REQUEST_THRESHOLD_MS) {
        VolleyLog.d("HTTP response for request=<%s> [lifetime=%d], [size=%s], " +
                "[rc=%d], [retryCount=%s]", request, requestLifetime,
                responseContents != null ? responseContents.length : "null",
                statusLine.getStatusCode(), request.getRetryPolicy().getCurrentRetryCount());
    }
}
 
Example 8
Source File: DiskBasedCache.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the specified key from the cache if it exists.
 */
@Override
public synchronized void remove(String key) {
    boolean deleted = getFileForKey(key).delete();
    removeEntry(key);
    if (!deleted) {
        VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
                key, getFilenameForKey(key));
    }
}
 
Example 9
Source File: BasicNetwork.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * Logs requests that took over SLOW_REQUEST_THRESHOLD_MS to complete.
 */
private void logSlowRequests(long requestLifetime, Request<?> request,
        byte[] responseContents, StatusLine statusLine) {
    if (DEBUG || requestLifetime > SLOW_REQUEST_THRESHOLD_MS) {
        VolleyLog.d("HTTP response for request=<%s> [lifetime=%d], [size=%s], " +
                "[rc=%d], [retryCount=%s]", request, requestLifetime,
                responseContents != null ? responseContents.length : "null",
                statusLine.getStatusCode(), request.getRetryPolicy().getCurrentRetryCount());
    }
}
 
Example 10
Source File: DiskBasedCache.java    From barterli_android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the cache entry with the specified key if it exists, null otherwise.
 */
@Override
public synchronized Entry get(String key) {
    CacheHeader entry = mEntries.get(key);
    // if the entry does not exist, return.
    if (entry == null) {
        return null;
    }

    File file = getFileForKey(key);
    CountingInputStream cis = null;
    try {
        cis = new CountingInputStream(new FileInputStream(file));
        CacheHeader.readHeader(cis); // eat header
        byte[] data = streamToBytes(cis, (int) (file.length() - cis.bytesRead));
        return entry.toCacheEntry(data);
    } catch (IOException e) {
        VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString());
        remove(key);
        return null;
    } finally {
        if (cis != null) {
            try {
                cis.close();
            } catch (IOException ioe) {
                return null;
            }
        }
    }
}
 
Example 11
Source File: DiskBasedCache.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the specified key from the cache if it exists.
 */
@Override
public synchronized void remove(String key) {
    boolean deleted = getFileForKey(key).delete();
    removeEntry(key);
    if (!deleted) {
        VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
                key, getFilenameForKey(key));
    }
}
 
Example 12
Source File: DiskBasedCache.java    From jus with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes the cache to fit the amount of bytes specified.
 * @param neededSpace The amount of bytes we are trying to fit into the cache.
 */
private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
    if (VolleyLog.DEBUG) {
        VolleyLog.v("Pruning old cache entries.");
    }

    long before = mTotalSize;
    int prunedFiles = 0;
    long startTime = System.currentTimeMillis();

    Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, CacheHeader> entry = iterator.next();
        CacheHeader e = entry.getValue();
        boolean deleted = getFileForKey(e.key).delete();
        if (deleted) {
            mTotalSize -= e.size;
        } else {
           VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
                   e.key, getFilenameForKey(e.key));
        }
        iterator.remove();
        prunedFiles++;

        if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }

    if (VolleyLog.DEBUG) {
        VolleyLog.v("pruned %d files, %d bytes, %d ms",
                prunedFiles, (mTotalSize - before), System.currentTimeMillis() - startTime);
    }
}
 
Example 13
Source File: BasicNetwork.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
/**
 * Logs requests that took over SLOW_REQUEST_THRESHOLD_MS to complete.
 */
private void logSlowRequests(long requestLifetime, Request<?> request,
        byte[] responseContents, StatusLine statusLine) {
    if (DEBUG || requestLifetime > SLOW_REQUEST_THRESHOLD_MS) {
        VolleyLog.d("HTTP response for request=<%s> [lifetime=%d], [size=%s], " +
                "[rc=%d], [retryCount=%s]", request, requestLifetime,
                responseContents != null ? responseContents.length : "null",
                statusLine.getStatusCode(), request.getRetryPolicy().getCurrentRetryCount());
    }
}
 
Example 14
Source File: BallRequest.java    From Volley-Ball with MIT License 5 votes vote down vote up
public void finish(final String tag) {
    if (mFinished) {
        throw new BallException("Trying to finish an already finished request");
    }

    mFinished = true;

    if (mRequestQueue != null) {
        mRequestQueue.finish(this);
    }

    if (BallMarkerLog.ENABLED) {
        final long threadId = Thread.currentThread().getId();
        if (Looper.myLooper() != Looper.getMainLooper()) {
            // If we finish marking off of the main thread, we need to
            // actually do it on the main thread to ensure correct ordering.
            Handler mainThread = new Handler(Looper.getMainLooper());
            mainThread.post(new Runnable() {
                @Override
                public void run() {
                    mEventLog.add(tag, threadId);
                    mEventLog.finish(this.toString());
                }
            });
            return;
        }

        mEventLog.add(tag, threadId);
        mEventLog.finish(this.toString());
    } else {
        long requestTime = SystemClock.elapsedRealtime() - mRequestBirthTime;
        if (requestTime >= SLOW_REQUEST_THRESHOLD_MS) {
            VolleyLog.d("%d ms: %s", requestTime, this.toString());
        }
    }
}
 
Example 15
Source File: DiskBasedCache.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes the cache to fit the amount of bytes specified.
 * @param neededSpace The amount of bytes we are trying to fit into the cache.
 */
private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
    if (VolleyLog.DEBUG) {
        VolleyLog.v("Pruning old cache entries.");
    }

    long before = mTotalSize;
    int prunedFiles = 0;
    long startTime = SystemClock.elapsedRealtime();

    Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, CacheHeader> entry = iterator.next();
        CacheHeader e = entry.getValue();
        boolean deleted = getFileForKey(e.key).delete();
        if (deleted) {
            mTotalSize -= e.size;
        } else {
           VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
                   e.key, getFilenameForKey(e.key));
        }
        iterator.remove();
        prunedFiles++;

        if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }

    if (VolleyLog.DEBUG) {
        VolleyLog.v("pruned %d files, %d bytes, %d ms",
                prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
    }
}
 
Example 16
Source File: DiskBasedCache.java    From SimplifyReader with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the cache entry with the specified key if it exists, null otherwise.
 */
@Override
public synchronized Entry get(String key) {
    CacheHeader entry = mEntries.get(key);
    // if the entry does not exist, return.
    if (entry == null) {
        return null;
    }

    File file = getFileForKey(key);
    CountingInputStream cis = null;
    try {
        cis = new CountingInputStream(new FileInputStream(file));
        CacheHeader.readHeader(cis); // eat header
        byte[] data = streamToBytes(cis, (int) (file.length() - cis.bytesRead));
        return entry.toCacheEntry(data);
    } catch (IOException e) {
        VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString());
        remove(key);
        return null;
    } finally {
        if (cis != null) {
            try {
                cis.close();
            } catch (IOException ioe) {
                return null;
            }
        }
    }
}
 
Example 17
Source File: DiskBasedCache.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the specified key from the cache if it exists.
 */
@Override
public synchronized void remove(String key) {
    boolean deleted = getFileForKey(key).delete();
    removeEntry(key);
    if (!deleted) {
        VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
                key, getFilenameForKey(key));
    }
}
 
Example 18
Source File: HttpTools.java    From volley with Apache License 2.0 4 votes vote down vote up
private void sendRequest(final int method, final RequestInfo requestInfo, final HttpCallback httpResult) {
    if (sRequestQueue == null) {
        sRequestQueue = Volley.newNoCacheRequestQueue(mContext);
    }
    if (httpResult != null) {
        httpResult.onStart();
    }
    if (requestInfo == null || TextUtils.isEmpty(requestInfo.url)) {
        if (httpResult != null) {
            httpResult.onError(new Exception("url can not be empty!"));
            httpResult.onFinish();
        }
        return;
    }
    switch (method) {
    case Request.Method.GET:
        requestInfo.url = requestInfo.getFullUrl();
        VolleyLog.d("get->%s", requestInfo.getUrl());
        break;
    case Request.Method.DELETE:
        requestInfo.url = requestInfo.getFullUrl();
        VolleyLog.d("delete->%s", requestInfo.getUrl());
        break;

    default:
        break;
    }
    final StringRequest request = new StringRequest(method, requestInfo.url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            if (httpResult != null) {
                httpResult.onResult(response);
                httpResult.onFinish();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) { 
            if (httpResult != null) {
                httpResult.onError(error);
                httpResult.onFinish();
            }
        }
    }, new Response.LoadingListener() {

        @Override
        public void onLoading(long count, long current) {
            if (httpResult != null) {
                httpResult.onLoading(count, current);
            }
        }
    }) {

        @Override
        public void cancel() {
            super.cancel();
            if (httpResult != null) {
                httpResult.onCancelled();
            }
        }
        
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            if (method == Request.Method.POST || method == Request.Method.PUT) {
                VolleyLog.d((method == Request.Method.POST ? "post->%s" : "put->%s"), requestInfo.getUrl() + ",params->" + requestInfo.getParams().toString());
                return requestInfo.getParams();
            } 
            return super.getParams();
        }
        
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return requestInfo.getHeaders();
        }
    };
    request.setTag(this);
    sRequestQueue.add(request);
}
 
Example 19
Source File: HttpTools.java    From volley with Apache License 2.0 4 votes vote down vote up
public DownloadRequest download(final RequestInfo requestInfo, String target, final boolean isResume, final HttpCallback httpResult) {
	final String url = requestInfo.getFullUrl();
	VolleyLog.d("download->%s", url);
    DownloadRequest request = new DownloadRequest(url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            if (httpResult != null) {
                httpResult.onResult(response);
                httpResult.onFinish();
            }
        }
    }, new Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            if (httpResult != null) {
                httpResult.onError(error);
                httpResult.onFinish();
            }
        }
        
    }, new Response.LoadingListener() {

        @Override
        public void onLoading(long count, long current) {
            if (httpResult != null) {
                httpResult.onLoading(count, current);
            }
        }
    }) {
        @Override
        public void stopDownload() {
            super.stopDownload();
            if (httpResult != null) {
                httpResult.onCancelled();
            }
        }
        
        @Override
        public void cancel() {
            super.cancel();
            if (httpResult != null) {
                httpResult.onCancelled();
            }
        }
        
        
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
        	Map<String, String> headers = super.getHeaders();
        	if (headers != null) {
        		headers.putAll(requestInfo.getHeaders());
        	} else {
        		headers = requestInfo.getHeaders();
        	}
            return headers;
        }
    };
    request.setResume(isResume);
    request.setTarget(target);
    if (httpResult != null) {
        httpResult.onStart();
    }
    if (TextUtils.isEmpty(url)) {
        if (httpResult != null) {
            httpResult.onError(new Exception("url can not be empty!"));
            httpResult.onFinish();
        }
        return request;
    }
    if (sDownloadQueue == null) {
        sDownloadQueue = Volley.newNoCacheRequestQueue(mContext);
    }
    sDownloadQueue.add(request);
    return request;

}
 
Example 20
Source File: OkHttpStack.java    From OkVolley with Apache License 2.0 4 votes vote down vote up
/**
     * perform the request
     *
     * @param request           request
     * @param additionalHeaders headers
     * @return http response
     * @throws java.io.IOException
     * @throws com.android.volley.AuthFailureError
     */
    @Override
    public Response performRequest(Request<?> request,
                                   Map<String, String> additionalHeaders) throws IOException, AuthFailureError {


        String url = request.getUrl();
        HashMap<String, String> map = new HashMap<String, String>();
        map.putAll(request.getHeaders());
        map.putAll(additionalHeaders);
        if (mUrlRewriter != null) {
            String rewritten = mUrlRewriter.rewriteUrl(url);
            if (rewritten == null) {
                throw new IOException("URL blocked by rewriter: " + url);
            }
            url = rewritten;
        }

        com.squareup.okhttp.Request.Builder builder = new com.squareup.okhttp.Request.Builder();
        builder.url(url);

        for (String headerName : map.keySet()) {
            builder.header(headerName, map.get(headerName));
//            connection.addRequestProperty(headerName, map.get(headerName));
            if (VolleyLog.DEBUG) {
                // print header message
                VolleyLog.d("RequestHeader: %1$s:%2$s", headerName, map.get(headerName));
            }
        }
        setConnectionParametersForRequest(builder, request);
        // Initialize HttpResponse with data from the okhttp.
        Response okHttpResponse = mClient.newCall(builder.build()).execute();

        int responseCode = okHttpResponse.code();
        if (responseCode == -1) {
            // -1 is returned by getResponseCode() if the response code could not be retrieved.
            // Signal to the caller that something was wrong with the connection.
            throw new IOException("Could not retrieve response code from HttpUrlConnection.");
        }
        return okHttpResponse;
    }