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

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:com.phildatoon.weather.YahooWeather.java

private String getWeatherString(Context context, String woeidNumber) {
    MyLog.d("query yahoo weather with WOEID number : " + woeidNumber);

    String qResult = "";
    String queryString = "http://weather.yahooapis.com/forecastrss?w=" + woeidNumber + "&u=f&d="
            + FORECAST_INFO_MAX_SIZE;/*from  ww  w  . j  av  a2 s .co m*/

    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(queryString);

    try {
        HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

        if (httpEntity != null) {
            InputStream inputStream = httpEntity.getContent();
            Reader in = new InputStreamReader(inputStream);
            BufferedReader bufferedreader = new BufferedReader(in);
            StringBuilder stringBuilder = new StringBuilder();

            String readLine = null;

            while ((readLine = bufferedreader.readLine()) != null) {
                MyLog.d(readLine);
                stringBuilder.append(readLine + "\n");
            }

            qResult = stringBuilder.toString();
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    }

    return qResult;
}

From source file:info.androidhive.androidsplashscreentimer.YahooWeather.java

private String getWeatherString(Context context, String woeidNumber) {
    MyLog.d("query yahoo weather with WOEID number : " + woeidNumber);

    String qResult = "";
    String queryString = "http://weather.yahooapis.com/forecastrss?w=" + woeidNumber;

    HttpClient httpClient = NetworkUtils.createHttpClient();

    HttpGet httpGet = new HttpGet(queryString);

    try {//  w ww. j  a v a  2s .c  o m
        HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

        if (httpEntity != null) {
            InputStream inputStream = httpEntity.getContent();
            Reader in = new InputStreamReader(inputStream);
            BufferedReader bufferedreader = new BufferedReader(in);
            StringBuilder stringBuilder = new StringBuilder();

            String readLine = null;

            while ((readLine = bufferedreader.readLine()) != null) {
                MyLog.d(readLine);
                stringBuilder.append(readLine + "\n");
            }

            qResult = stringBuilder.toString();
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    } catch (ConnectTimeoutException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    } catch (SocketTimeoutException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    } finally {
        httpClient.getConnectionManager().shutdown();
    }

    return qResult;
}

From source file:net.issarlk.androbunny.inkbunny.API.java

public User getUserFromName(String username) {
    String tmps = "";
    try {//from  w  w w.  ja v  a 2s. c  o m
        //Use the user autocomplete API to retrieve user
        tmps = AndrobunnyAppSingleton.androbunnyapp
                .readUri(new URI("https://inkbunny.net/api_username_autosuggest.php?username="
                        + URLEncoder.encode(username, "UTF-8")));
    } catch (ClientProtocolException e) {
        Log.e(TAG, e.toString());
        return null;
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, e.toString());
        return null;
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        return null;
    } catch (URISyntaxException e) {
        Log.e(TAG, e.toString());
        return null;
    }
    try {
        JSONObject json_response;
        json_response = (JSONObject) new JSONTokener(tmps).nextValue();
        JSONArray suggestions = json_response.getJSONArray("results");
        //Use first suggestion
        JSONObject suggestion = suggestions.getJSONObject(0);
        return new User(suggestion.getInt("id"), suggestion.getString("singleword"));
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        return null;
    }
}

From source file:net.issarlk.androbunny.inkbunny.API.java

public String[] getKeywords(String text) {
    //Search keywords starting with text
    String tmps;/*www. j a  v a 2  s  .  c  om*/
    try {
        tmps = AndrobunnyAppSingleton.androbunnyapp
                .readUri(new URI("https://inkbunny.net/api_search_autosuggest.php?ratingsmask="
                        + this.ratingsmask + "&keyword=" + URLEncoder.encode(text, "UTF-8")));
    } catch (ClientProtocolException e) {
        Log.e(TAG, e.toString());
        return null;
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, e.toString());
        return null;
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        return null;
    } catch (URISyntaxException e) {
        Log.e(TAG, e.toString());
        return null;
    }
    try {
        JSONObject json_response;
        json_response = (JSONObject) new JSONTokener(tmps).nextValue();
        JSONArray suggestions = json_response.getJSONArray("results");
        int l = suggestions.length();
        String[] result = new String[l];
        for (int i = 0; i < l; i++) {
            JSONObject suggestion;
            suggestion = suggestions.getJSONObject(i);
            result[i] = suggestion.getString("singleword");
        }
        return result;
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        return null;
    }
}

From source file:net.issarlk.androbunny.inkbunny.API.java

public Submission getSubmissionByID(int id) throws IOException {
    try {/*  w  w w  .  ja  v a  2s.  co  m*/
        //Load submission details         
        String tmps = AndrobunnyAppSingleton.androbunnyapp
                .readUri(new URI("https://inkbunny.net/api_submissions.php?sid=" + this.sid.get()
                        + "&show_description_bbcode_parsed=yes&show_writing_bbcode_parsed=yes&submission_ids="
                        + id));
        JSONObject json = (JSONObject) new JSONTokener(tmps).nextValue();
        JSONArray submissions = json.getJSONArray("submissions");
        //There should only be one submission in the array, the one we are looking for
        json = submissions.getJSONObject(0);
        return new Submission(json);
    } catch (ClientProtocolException e) {
        Log.e(TAG, "Exception: " + e.toString());
        return null;
    } catch (URISyntaxException e) {
        Log.e(TAG, "Exception: " + e.toString());
        return null;
    } catch (JSONException e) {
        Log.e(TAG, "Exception: " + e.toString());
        return null;
    }
}

From source file:com.example.apis.ifashion.YahooWeather.java

private String getWeatherString(Context context, String woeidNumber) {
    YahooWeatherLog.d("query yahoo weather with WOEID number : " + woeidNumber);

    String qResult = "";
    String queryString = "http://weather.yahooapis.com/forecastrss?w=" + woeidNumber;

    HttpClient httpClient = NetworkUtils.createHttpClient();

    HttpGet httpGet = new HttpGet(queryString);

    try {//from   w  w  w.j  av  a2s  . com
        HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

        if (httpEntity != null) {
            InputStream inputStream = httpEntity.getContent();
            Reader in = new InputStreamReader(inputStream);
            BufferedReader bufferedreader = new BufferedReader(in);
            StringBuilder stringBuilder = new StringBuilder();

            String readLine = null;

            while ((readLine = bufferedreader.readLine()) != null) {
                YahooWeatherLog.d(readLine);
                stringBuilder.append(readLine + "\n");
            }

            qResult = stringBuilder.toString();
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    } catch (ConnectTimeoutException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    } catch (SocketTimeoutException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    } finally {
        httpClient.getConnectionManager().shutdown();
    }

    return qResult;
}

From source file:edu.rit.csh.androidwebnews.HttpsGetAsyncTask.java

/**
 * The method that gets run when execute() is run. This sends the URL with the
 * GEt parameters to the server and gets the results
 *
 * @param params - [0] is the URL to got to
 * @return String representation of page results
 *//*from  w w  w .  jav  a 2  s . c  o m*/
@Override
protected String doInBackground(URI... params) {
    String page;
    BufferedReader in;
    HttpResponse response;
    HttpGet request;

    try {
        request = new HttpGet(params[0]);
        request.addHeader("accept", "application/json");
        response = httpclient.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuilder sb = new StringBuilder("");
        String line;
        String NL = System.getProperty("line.separator");

        while ((line = in.readLine()) != null) {
            sb.append(line).append(NL);
        }
        page = sb.toString();
        in.close();
        Log.d("jd - getAsync response", page);
    } catch (ClientProtocolException e) {
        return ("Error: Client Exception while connecting to " + params[0].getHost());
    } catch (HttpHostConnectException e) {
        return ("Error: Could not connect to " + params[0].getHost()
                + ". Mostly likely caused by the server not being up");
    } catch (ConnectTimeoutException e) { // redoes the request if there is a timeout
        Log.d("jd - getAsync Error", e.toString());
        return ("Error: Connection timeout");
    } catch (IOException e) {
        Log.d("jd - GetAsync Error", e.toString());
        return ("Error: IO Connection exception while connection to " + params[0].getHost());
    }

    return page;
}

From source file:com.gaze.webpaser.StackWidgetService.java

private void getDatafromNetwork() {

    new AsyncTask<String, Void, String>() {

        @Override/*  w  w w .j av  a2 s.c  om*/
        protected void onPreExecute() {

            super.onPreExecute();
            onStartDataLoading();
        }

        @Override
        protected String doInBackground(String... params) {

            ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

            try {
                // Set up HTTP post

                // HttpClient is more then less deprecated. Need to change
                // to
                // URLConnection
                HttpClient httpClient = new DefaultHttpClient();

                HttpPost httpPost = new HttpPost(listUrl);
                httpPost.setEntity(new UrlEncodedFormEntity(param));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();

                Log.i(LOG_TAG, "finish http");

                // Read content & Log
                inputStream = httpEntity.getContent();
            } catch (UnsupportedEncodingException e1) {
                Log.e("UnsupportedEncodingException", e1.toString());
                e1.printStackTrace();
            } catch (ClientProtocolException e2) {
                Log.e("ClientProtocolException", e2.toString());
                e2.printStackTrace();
            } catch (IllegalStateException e3) {
                Log.e("IllegalStateException", e3.toString());
                e3.printStackTrace();
            } catch (IOException e4) {
                Log.e("IOException", e4.toString());
                e4.printStackTrace();
            }
            // Convert response to string using String Builder
            try {
                BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"),
                        8);
                StringBuilder sBuilder = new StringBuilder();

                String line = null;
                while ((line = bReader.readLine()) != null) {
                    sBuilder.append(line + "\n");
                }

                inputStream.close();
                result = sBuilder.toString();

                Log.i(LOG_TAG, "finish read stream: " + result);
                if (!isStreamTheTargetJson(result)) {
                    result = "";
                    sBuilder.delete(0, sBuilder.length() - 1);
                }

                // parse json string here
                if (!result.isEmpty()) {
                    if (result.startsWith("<html>"))
                        return "";

                    JSONObject titleJson = new JSONObject(result);
                    JSONArray datajson = titleJson.getJSONArray("data");
                    JSONArray urlQueue = datajson.getJSONArray(0);

                    for (int i = 0; i < urlQueue.length(); i++) {
                        JSONObject item = urlQueue.getJSONObject(i);
                        String url = item.getString("link");
                        String introtext = item.getString("introtext");
                        String title = item.getString("title");
                        String images = item.getString("images");
                        String name = item.getString("name");
                        String time = item.getString("publish_up");
                        if (url != null) {
                            // addToQueue(GlobalData.baseUrl+'/'+url);
                            addToList(url, introtext, title, images, name, time);
                        }
                    }
                }

            } catch (Exception e) {
                Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());

            }
            return result;
        }

        @Override
        protected void onPostExecute(String msg) {
            // mDisplay.append(msg + "\n");
            Log.i(LOG_TAG, msg.toString());
            onFinishDataLoading();

        }

    }.execute();
}

From source file:cn.code.notes.gtask.remote.GTaskClient.java

private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
    if (!mLoggedin) {
        Log.e(TAG, "please login first");
        throw new ActionFailureException("not logged in");
    }//w  w w . j ava 2 s.c  o m

    HttpPost httpPost = createHttpPost();
    try {
        LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
        list.add(new BasicNameValuePair("r", js.toString()));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
        httpPost.setEntity(entity);

        // execute the post
        HttpResponse response = mHttpClient.execute(httpPost);
        String jsString = getResponseContent(response.getEntity());
        return new JSONObject(jsString);

    } catch (ClientProtocolException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("postRequest failed");
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("postRequest failed");
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("unable to convert response content to jsonobject");
    } catch (Exception e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("error occurs when posting request");
    }
}

From source file:cn.code.notes.gtask.remote.GTaskClient.java

public JSONArray getTaskLists() throws NetworkFailureException {
    if (!mLoggedin) {
        Log.e(TAG, "please login first");
        throw new ActionFailureException("not logged in");
    }//from  w w w .java2s .  c  o  m

    try {
        HttpGet httpGet = new HttpGet(mGetUrl);
        HttpResponse response = null;
        response = mHttpClient.execute(httpGet);

        // get the task list
        String resString = getResponseContent(response.getEntity());
        String jsBegin = "_setup(";
        String jsEnd = ")}</script>";
        int begin = resString.indexOf(jsBegin);
        int end = resString.lastIndexOf(jsEnd);
        String jsString = null;
        if (begin != -1 && end != -1 && begin < end) {
            jsString = resString.substring(begin + jsBegin.length(), end);
        }
        JSONObject js = new JSONObject(jsString);
        return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
    } catch (ClientProtocolException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("gettasklists: httpget failed");
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("gettasklists: httpget failed");
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("get task lists: handing jasonobject failed");
    }
}