Example usage for org.apache.http.client ClientProtocolException printStackTrace

List of usage examples for org.apache.http.client ClientProtocolException printStackTrace

Introduction

In this page you can find the example usage for org.apache.http.client ClientProtocolException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:org.cmuchimps.gort.modules.webinfoservice.AppEngineUpload.java

private static String uploadBlobstoreDataNoRetry(String url, String filename, String mime, byte[] data) {
    if (url == null || url.length() <= 0) {
        return null;
    }/*from  www  . jav  a  2  s  .c  o  m*/

    if (data == null || data.length <= 0) {
        return null;
    }

    HttpClient httpClient = new DefaultHttpClient();

    HttpPost httpPost = new HttpPost(url);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart("data", new ByteArrayBody(data, mime, filename));

    httpPost.setEntity(entity);

    try {
        HttpResponse response = httpClient.execute(httpPost);

        System.out.println("Blob upload status code: " + response.getStatusLine().getStatusCode());

        /*
        //http://grinder.sourceforge.net/g3/script-javadoc/HTTPClient/HTTPResponse.html
        // 2xx - success
        if (response.getStatusLine().getStatusCode() / 100 != 2) {
            return null;
        }
        */

        InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
        BufferedReader br = new BufferedReader(isr);

        String blobKey = br.readLine();

        blobKey = (blobKey != null) ? blobKey.trim() : null;

        br.close();
        isr.close();

        if (blobKey != null && blobKey.length() > 0) {
            return String.format("%s%s", MTURKSERVER_BLOB_DOWNLOAD_URL, blobKey);
        } else {
            return null;
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.foobnix.api.vkontakte.VkOld.java

public static List<VkAudio> searchAll(String text, Context context) throws VKAuthorizationException {

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("q", text));
    params.add(new BasicNameValuePair("count", "100"));
    params.add(new BasicNameValuePair("access_token", Pref.getStr(context, Pref.VKONTAKTE_TOKEN)));

    String paramsList = URLEncodedUtils.format(params, "UTF-8");
    HttpGet request = new HttpGet(API_URL + "audio.search?" + paramsList);

    HttpClient client = new DefaultHttpClient();
    HttpResponse response;/*from   w ww. ja v  a  2s .  co  m*/
    List<VkAudio> result = null;
    try {
        response = client.execute(request);

        HttpEntity entity = response.getEntity();
        String jString = EntityUtils.toString(entity);

        if (jString.contains("error_code")) {
            throw new VKAuthorizationException("VK connection Erorr");
        }

        LOG.d(jString);

        result = JSONHelper.parseVKSongs(jString);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return result;

}

From source file:com.kku.apps.pricesearch.util.HttpConnection.java

public static Bitmap getBitmapFromUrl(String url) {

    HttpGet method = new HttpGet(url);
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {//from  w w w  .  j  a  v  a 2s  .c  om
        BufferedHttpEntity entity = httpClient.execute(method, new ResponseHandler<BufferedHttpEntity>() {

            @Override
            public BufferedHttpEntity handleResponse(HttpResponse response)
                    throws ClientProtocolException, IOException {

                // Xe?[^XR?[h
                int status = response.getStatusLine().getStatusCode();

                if (HttpStatus.SC_OK != status) {
                    throw new RuntimeException("?MG?[?");
                }
                HttpEntity entity = response.getEntity();
                BufferedHttpEntity bufferEntity = new BufferedHttpEntity(entity);
                return bufferEntity;
            }
        });
        InputStream is = entity.getContent();
        return BitmapFactory.decodeStream(is);
    } catch (ClientProtocolException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:it.sasabz.android.sasabus.classes.hafas.XMLRequest.java

/**
 * Sends a HTTP-Post request to the xml-interface of the hafas travelplanner
 * @param xml is the xml-file containing a xml-request for the hafas travelplanner
 * @return the response of the hafas travelplanner xml interface
 */// www .  j  a  v a 2 s.  c o  m
private static String execute(String xml) {
    String ret = "";
    if (!haveNetworkConnection()) {
        return ret;
    }
    try {
        HttpClient http = new DefaultHttpClient();
        HttpPost post = new HttpPost(SASAbus.getContext().getString(R.string.xml_server));
        StringEntity se = new StringEntity(xml, HTTP.UTF_8);
        se.setContentType("text/xml");
        post.setEntity(se);

        HttpResponse response = http.execute(post);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            ret = EntityUtils.toString(response.getEntity());
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ret;
}

From source file:fr.forexperts.service.DataService.java

private static ArrayList<String[]> getCsvFromUrl(String url) {
    ArrayList<String[]> data = new ArrayList<String[]>();

    try {/*from w  w w.j a  v  a  2  s.  com*/
        HttpGet httpGet = new HttpGet(url);
        HttpParams httpParameters = new BasicHttpParams();
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

        HttpResponse response = httpClient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();

        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));

            String line;
            while ((line = reader.readLine()) != null) {
                String[] price = line.split(",");
                data.add(price);
            }
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return data;
}

From source file:messenger.YahooFinanceAPI.java

public static String httpget(String url) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    if (use_proxy) {
        HttpHost proxy = new HttpHost(PROXY_NAME, PROXY_PORT);
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }/*from  www.  j a  v a2s .c o  m*/

    HttpGet httpget = new HttpGet(url);
    // Override the default policy for this request

    httpget.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

    HttpResponse response = null;
    String responseString = null;
    try {
        response = httpclient.execute(httpget);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            cookiestore = httpclient.getCookieStore();
            responseString = EntityUtils.toString(response.getEntity());
            // pG^O 200 OK ~X
            // System.out.println(responseString);
            //
        } else {
            System.out.println(response.getStatusLine());
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    httpclient.getConnectionManager().shutdown();
    return responseString;
}

From source file:com.speakingcode.freemusicarchive.api.FMAConnector.java

/**
 * Makes the HTTP GET request to the provided REST url 
 * @param requestUrl the URL to request/*from   ww  w .  j  a va2 s.c o  m*/
 * @return The string of the response from the HTTP request
 */
public static String callWebService(String requestUrl) {
    String deviceId = "xxxxx";

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet request = new HttpGet(requestUrl);
    request.addHeader("deviceId", deviceId);

    ResponseHandler<String> handler = new BasicResponseHandler();
    String result = "";

    try {
        result = httpclient.execute(request, handler);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Log.e(TAG, "ClientProtocolException in callWebService(). " + e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(TAG, "IOException in callWebService(). " + e.getMessage());
    }

    httpclient.getConnectionManager().shutdown();
    Log.i(TAG, "**callWebService() successful. Result: **");
    Log.i(TAG, result);
    Log.i(TAG, "*****************************************");

    return result;
}

From source file:realizer.com.schoolgenieparent.ServerUtilities.java

private static StringBuilder postUnregister(String EmpId, String did, String access) {

    String my = Config.URL + "deregisterStudentDevice/" + EmpId + "/" + did;
    Log.d("GCMDID", my);
    builder = new StringBuilder();
    HttpGet httpGet = new HttpGet(my);
    HttpClient client = new DefaultHttpClient();
    httpGet.setHeader("AccessToken", access);
    try {/* w  w  w.j a  va 2 s  . com*/
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();

        int statusCode = statusLine.getStatusCode();
        Log.d("GCMSTATUS", "" + statusCode);
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e("Error", "Failed to Login");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        client.getConnectionManager().closeExpiredConnections();
        client.getConnectionManager().shutdown();
    }

    return builder;
}

From source file:realizer.com.schoolgenieparent.ServerUtilities.java

private static StringBuilder post(String regID, String EmpId) {
    //String my = "http://104.217.254.180/RestWCF/svcEmp.svc/registerDevice/" + EmpId + "/" + regID;
    SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
    String did = sharedpreferences.getString("DWEVICEID", "");

    //String my = "http://192.168.1.14/SJRestWCF/registerDevice/" + EmpId + "/" + regID;
    String my = Config.URL + "RegisterStudentDevice/" + EmpId + "/" + did + "/" + regID;
    Log.d("GCMDID", my);
    builder = new StringBuilder();
    HttpGet httpGet = new HttpGet(my);
    HttpClient client = new DefaultHttpClient();
    try {//from w w w  . j  a  va 2 s  .  c om
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();

        int statusCode = statusLine.getStatusCode();
        Log.d("GCMSTATUS", "" + statusCode);
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e("Error", "Failed to Login");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        client.getConnectionManager().closeExpiredConnections();
        client.getConnectionManager().shutdown();
    }

    return builder;
}

From source file:org.vuphone.assassins.android.http.HTTPGetter.java

private static ArrayList<LandMine> handleLandMineResponse(HttpGet get) {

    HttpResponse resp;//from   ww  w  .  j ava 2  s  .  c  om

    // Execute the get
    try {
        resp = c.execute(get);
    } catch (ClientProtocolException e1) {
        e1.printStackTrace();
        Log.e(VUphone.tag, pre + "HTTP error while executing post");
        return null;
    } catch (SocketException se) {
        // If we have no Internet connection, we don't want to wipe the
        // existing list of land mines by returning null.
        Log.e(VUphone.tag, pre + "SocketException: handled by " + "returning current land mine list.");
        se.printStackTrace();
        return GameObjects.getInstance().getLandMines();
    } catch (IOException e1) {
        e1.printStackTrace();
        Log.e(VUphone.tag, pre + "HTTP error in server response");
        return null;
    } catch (Exception e) {
        Log.e(VUphone.tag, pre + "An unknown exception was thrown");
        e.printStackTrace();
        return null;
    }

    Log.i(VUphone.tag, pre + "HTTP operation complete. Processing response.");

    // Convert Response Entity to usable format
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    try {
        resp.getEntity().writeTo(bao);
        Log.v(VUphone.tag, pre + "Http response: " + bao);
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(VUphone.tag, pre + "Unable to write response to byte[]");
        return null;
    }

    // Extract Land Mines from response
    if (bao.size() == 0) {
        Log.w(VUphone.tag,
                pre + "Response was completely empty, " + "are you sure you are using the "
                        + "same version client and server? " + "At the least, there should have "
                        + "been empty XML here");
    }

    ArrayList<LandMine> mines = new ArrayList<LandMine>(
            landMineHandler_.processXML(new InputSource(new ByteArrayInputStream(bao.toByteArray()))));

    return mines;
    //return GameObjects.getInstance().getLandMines();
    //return null;
}