Search in sources :

Example 1 with HttpResponse

use of cz.msebera.android.httpclient.HttpResponse in project AndroidStudy by tinggengyan.

the class HttpCookie method run.

@Override
public void run() {
    list.add(new BasicNameValuePair("name", "steve"));
    list.add(new BasicNameValuePair("age", "12"));
    try {
        post.setEntity(new UrlEncodedFormEntity(list));
        HttpResponse response = client.execute(post);
        if (response.getStatusLine().getStatusCode() == 200) {
            AbstractHttpClient absclient = (AbstractHttpClient) client;
            List<Cookie> cookies = absclient.getCookieStore().getCookies();
            for (Cookie cookie : cookies) {
                System.out.println("name==" + cookie.getName());
                System.out.println("value==" + cookie.getValue());
                Message message = new Message();
                message.obj = cookie;
                mHandler.sendMessage(message);
                return;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : AbstractHttpClient(cz.msebera.android.httpclient.impl.client.AbstractHttpClient) Cookie(cz.msebera.android.httpclient.cookie.Cookie) Message(android.os.Message) BasicNameValuePair(cz.msebera.android.httpclient.message.BasicNameValuePair) HttpResponse(cz.msebera.android.httpclient.HttpResponse) UrlEncodedFormEntity(cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity)

Example 2 with HttpResponse

use of cz.msebera.android.httpclient.HttpResponse in project Simple-Dilbert by smarek.

the class GetStripUrl method doInBackground.

@Override
protected String[] doInBackground(Void... params) {
    if (this.currDate == null) {
        Log.e(TAG, "Cannot load for null date");
        return null;
    }
    String cached = this.preferences.getCachedUrl(this.currDate);
    if (cached != null) {
        return new String[] { cached, this.preferences.getCachedTitle(this.currDate) };
    }
    HttpGet get = new HttpGet("http://dilbert.com/strip/" + currDate.toString(DilbertPreferences.DATE_FORMATTER) + "/");
    HttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        client = HttpClients.createSystem();
        response = client.execute(get);
    } catch (Exception e) {
        Log.e(TAG, "HttpGet failed", e);
    }
    if (response == null) {
        return null;
    }
    String[] rtn = handleParse(response);
    try {
        client.close();
    } catch (IOException e) {
        Log.e(TAG, "Closing HttpClient failed", e);
    }
    return rtn;
}
Also used : CloseableHttpClient(cz.msebera.android.httpclient.impl.client.CloseableHttpClient) HttpGet(cz.msebera.android.httpclient.client.methods.HttpGet) HttpResponse(cz.msebera.android.httpclient.HttpResponse) IOException(java.io.IOException) IOException(java.io.IOException) NetworkErrorException(android.accounts.NetworkErrorException)

Example 3 with HttpResponse

use of cz.msebera.android.httpclient.HttpResponse in project Zom-Android by zom.

the class OtrDataHandler method onIncomingResponse.

public void onIncomingResponse(Address from, Address to, byte[] value) {
    // Log.e( TAG, "onIncomingResponse:" + value.length);
    SessionInputBuffer buffer = new MemorySessionInputBuffer(value);
    HttpResponseParser parser = new HttpResponseParser(buffer, lineParser, responseFactory, params);
    HttpResponse res;
    try {
        res = (HttpResponse) parser.parse();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (HttpException e) {
        e.printStackTrace();
        return;
    }
    String uid = res.getFirstHeader("Request-Id").getValue();
    Request request = requestCache.get(uid);
    if (request == null) {
        debug("Unknown request ID " + uid);
        return;
    }
    if (request.isSeen()) {
        debug("Already seen request ID " + uid);
        return;
    }
    request.seen();
    int statusCode = res.getStatusLine().getStatusCode();
    if (statusCode != 200) {
        debug("got status " + statusCode + ": " + res.getStatusLine().getReasonPhrase());
        // TODO handle error
        return;
    }
    // TODO handle success
    try {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        readIntoByteBuffer(byteBuffer, buffer);
        // debug("Received sha1 @" + request.start + " is " + sha1sum(byteBuffer.toByteArray()));
        if (request.method.equals("GET")) {
            Transfer transfer = transferCache.get(request.url);
            if (transfer == null) {
                debug("Transfer expired for url " + request.url);
                return;
            }
            transfer.chunkReceived(request, byteBuffer.toByteArray());
            if (transfer.isDone()) {
                // Log.e( TAG, "onIncomingResponse: isDone");
                debug("Transfer complete for " + request.url);
                String filename = transfer.closeFile();
                Uri vfsUri = SecureMediaStore.vfsUri(filename);
                if (transfer.checkSum()) {
                    // Log.e( TAG, "onIncomingResponse: writing");
                    if (mDataListener != null)
                        mDataListener.onTransferComplete(false, null, from.getAddress(), transfer.url, transfer.type, vfsUri.toString());
                } else {
                    if (mDataListener != null)
                        mDataListener.onTransferFailed(false, null, to.getAddress(), transfer.url, "checksum");
                    debug("Wrong checksum for file");
                }
            } else {
                if (mDataListener != null)
                    mDataListener.onTransferProgress(true, null, to.getAddress(), transfer.url, ((float) transfer.chunksReceived) / transfer.chunks);
                transfer.perform();
                debug("Progress " + transfer.chunksReceived + " / " + transfer.chunks);
            }
        }
    } catch (IOException e) {
        debug("Could not read line from response");
    } catch (RemoteException e) {
        debug("Could not read remote exception");
    }
}
Also used : AbstractSessionInputBuffer(cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer) SessionInputBuffer(cz.msebera.android.httpclient.io.SessionInputBuffer) HttpRequest(cz.msebera.android.httpclient.HttpRequest) BasicHttpRequest(cz.msebera.android.httpclient.message.BasicHttpRequest) HttpResponse(cz.msebera.android.httpclient.HttpResponse) BasicHttpResponse(cz.msebera.android.httpclient.message.BasicHttpResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Uri(android.net.Uri) HttpResponseParser(cz.msebera.android.httpclient.impl.io.HttpResponseParser) HttpException(cz.msebera.android.httpclient.HttpException) RemoteException(android.os.RemoteException)

Example 4 with HttpResponse

use of cz.msebera.android.httpclient.HttpResponse in project android-async-http by loopj.

the class AsyncHttpRequest method makeRequest.

private void makeRequest() throws IOException {
    if (isCancelled()) {
        return;
    }
    // Fixes #115
    if (request.getURI().getScheme() == null) {
        // subclass of IOException so processed in the caller
        throw new MalformedURLException("No valid URI scheme was provided");
    }
    if (responseHandler instanceof RangeFileAsyncHttpResponseHandler) {
        ((RangeFileAsyncHttpResponseHandler) responseHandler).updateRequestHeaders(request);
    }
    HttpResponse response = client.execute(request, context);
    if (isCancelled()) {
        return;
    }
    // Carry out pre-processing for this response.
    responseHandler.onPreProcessResponse(responseHandler, response);
    if (isCancelled()) {
        return;
    }
    // The response is ready, handle it.
    responseHandler.sendResponseMessage(response);
    if (isCancelled()) {
        return;
    }
    // Carry out post-processing for this response.
    responseHandler.onPostProcessResponse(responseHandler, response);
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpResponse(cz.msebera.android.httpclient.HttpResponse)

Aggregations

HttpResponse (cz.msebera.android.httpclient.HttpResponse)4 IOException (java.io.IOException)2 NetworkErrorException (android.accounts.NetworkErrorException)1 Uri (android.net.Uri)1 Message (android.os.Message)1 RemoteException (android.os.RemoteException)1 HttpException (cz.msebera.android.httpclient.HttpException)1 HttpRequest (cz.msebera.android.httpclient.HttpRequest)1 UrlEncodedFormEntity (cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity)1 HttpGet (cz.msebera.android.httpclient.client.methods.HttpGet)1 Cookie (cz.msebera.android.httpclient.cookie.Cookie)1 AbstractHttpClient (cz.msebera.android.httpclient.impl.client.AbstractHttpClient)1 CloseableHttpClient (cz.msebera.android.httpclient.impl.client.CloseableHttpClient)1 AbstractSessionInputBuffer (cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer)1 HttpResponseParser (cz.msebera.android.httpclient.impl.io.HttpResponseParser)1 SessionInputBuffer (cz.msebera.android.httpclient.io.SessionInputBuffer)1 BasicHttpRequest (cz.msebera.android.httpclient.message.BasicHttpRequest)1 BasicHttpResponse (cz.msebera.android.httpclient.message.BasicHttpResponse)1 BasicNameValuePair (cz.msebera.android.httpclient.message.BasicNameValuePair)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1