Example usage for java.io UnsupportedEncodingException toString

List of usage examples for java.io UnsupportedEncodingException toString

Introduction

In this page you can find the example usage for java.io UnsupportedEncodingException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:com.streaming.sweetplayer.utils.JSONParser.java

public JSONObject getJSONFromUrl(String url) {
    // Making HTTP request
    try {/*w  ww .  j  av a2  s .  c  om*/
        // DefaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
}

From source file:com.video_browser_thesis.json.JSONParser.java

/**
 * Makes the HTTP request, parses the JSON String, creates and JSON Object 
 * @param httpClient   //from  www.  java  2s . c om
 * @param httpPost
 * @param httpResponse
 * @param httpEntity
 * @param reader
 * @param sb
 * @param line
 * @return jObj
 * @see JSONObject
 */
public JSONObject getJSONFromUrl(String url) {

    DefaultHttpClient httpClient;
    HttpPost httpPost;
    HttpResponse httpResponse;
    HttpEntity httpEntity;
    BufferedReader reader;
    StringBuilder sb;
    String line;

    try {
        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(url);

        httpResponse = httpClient.execute(httpPost);
        httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        sb = new StringBuilder();
        line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return jObj;
}

From source file:com.lgallardo.qbittorrentclient.RSSFeedParser.java

public RSSFeed getRSSFeed(String channelTitle, String channelUrl, String filter) {

    // Decode url link
    try {/*from  ww w  . j  a va  2 s  . c o m*/
        channelUrl = URLDecoder.decode(channelUrl, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        Log.e("Debug", "RSSFeedParser - decoding error: " + e.toString());
    }

    // Parse url
    Uri uri = uri = Uri.parse(channelUrl);
    ;
    int event;
    String text = null;
    String torrent = null;
    boolean header = true;

    // TODO delete itemCount, as it's not really used
    this.itemCount = 0;

    HttpResponse httpResponse;
    DefaultHttpClient httpclient;

    XmlPullParserFactory xmlFactoryObject;
    XmlPullParser xmlParser = null;

    HttpParams httpParameters = new BasicHttpParams();

    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = connection_timeout * 1000;

    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = data_timeout * 1000;

    // Set http parameters
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    HttpProtocolParams.setUserAgent(httpParameters, "qBittorrent for Android");
    HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);

    RSSFeed rssFeed = new RSSFeed();
    rssFeed.setChannelTitle(channelTitle);
    rssFeed.setChannelLink(channelUrl);

    httpclient = null;

    try {

        // Making HTTP request
        HttpHost targetHost = new HttpHost(uri.getAuthority());

        // httpclient = new DefaultHttpClient(httpParameters);
        // httpclient = new DefaultHttpClient();
        httpclient = getNewHttpClient();

        httpclient.setParams(httpParameters);

        //            AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        //            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(this.username, this.password);
        //
        //            httpclient.getCredentialsProvider().setCredentials(authScope, credentials);

        // set http parameters

        HttpGet httpget = new HttpGet(channelUrl);

        httpResponse = httpclient.execute(targetHost, httpget);

        StatusLine statusLine = httpResponse.getStatusLine();

        int mStatusCode = statusLine.getStatusCode();

        if (mStatusCode != 200) {
            httpclient.getConnectionManager().shutdown();
            throw new JSONParserStatusCodeException(mStatusCode);
        }

        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

        xmlFactoryObject = XmlPullParserFactory.newInstance();
        xmlParser = xmlFactoryObject.newPullParser();

        xmlParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        xmlParser.setInput(is, null);

        event = xmlParser.getEventType();

        // Get Channel info
        String name;
        RSSFeedItem item = null;
        ArrayList<RSSFeedItem> items = new ArrayList<RSSFeedItem>();

        // Get items
        while (event != XmlPullParser.END_DOCUMENT) {

            name = xmlParser.getName();

            switch (event) {
            case XmlPullParser.START_TAG:

                if (name != null && name.equals("item")) {
                    header = false;
                    item = new RSSFeedItem();
                    itemCount = itemCount + 1;
                }

                try {
                    for (int i = 0; i < xmlParser.getAttributeCount(); i++) {

                        if (xmlParser.getAttributeName(i).equals("url")) {
                            torrent = xmlParser.getAttributeValue(i);

                            if (torrent != null) {
                                torrent = Uri.decode(URLEncoder.encode(torrent, "UTF-8"));
                            }
                            break;
                        }
                    }
                } catch (Exception e) {

                }

                break;

            case XmlPullParser.TEXT:
                text = xmlParser.getText();
                break;

            case XmlPullParser.END_TAG:

                if (name.equals("title")) {
                    if (!header) {
                        item.setTitle(text);
                        //                                Log.d("Debug", "PARSER - Title: " + text);
                    }
                } else if (name.equals("description")) {
                    if (header) {
                        //                                Log.d("Debug", "Channel Description: " + text);
                    } else {
                        item.setDescription(text);
                        //                                Log.d("Debug", "Description: " + text);
                    }
                } else if (name.equals("link")) {
                    if (!header) {
                        item.setLink(text);
                        //                                Log.d("Debug", "Link: " + text);
                    }

                } else if (name.equals("pubDate")) {

                    // Set item pubDate
                    if (item != null) {
                        item.setPubDate(text);
                    }

                } else if (name.equals("enclosure")) {
                    item.setTorrentUrl(torrent);
                    //                            Log.d("Debug", "Enclosure: " + torrent);
                } else if (name.equals("item") && !header) {

                    if (items != null & item != null) {

                        // Fix torrent url for no-standard rss feeds
                        if (torrent == null) {

                            String link = item.getLink();

                            if (link != null) {
                                link = Uri.decode(URLEncoder.encode(link, "UTF-8"));
                            }

                            item.setTorrentUrl(link);
                        }

                        items.add(item);
                    }

                }

                break;
            }

            event = xmlParser.next();

            //                if (!header) {
            //                    items.add(item);
            //                }

        }

        // Filter items

        //            Log.e("Debug", "RSSFeedParser - filter: >" + filter + "<");
        if (filter != null && !filter.equals("")) {

            Iterator iterator = items.iterator();

            while (iterator.hasNext()) {

                item = (RSSFeedItem) iterator.next();

                // If link doesn't match filter, remove it
                //                    Log.e("Debug", "RSSFeedParser - item no filter: >" + item.getTitle() + "<");

                Pattern patter = Pattern.compile(filter);

                Matcher matcher = patter.matcher(item.getTitle()); // get a matcher object

                if (!(matcher.find())) {
                    iterator.remove();
                }

            }
        }

        rssFeed.setItems(items);
        rssFeed.setItemCount(itemCount);
        rssFeed.setChannelPubDate(items.get(0).getPubDate());
        rssFeed.setResultOk(true);

        is.close();
    } catch (Exception e) {
        Log.e("Debug", "RSSFeedParser - : " + e.toString());
        rssFeed.setResultOk(false);
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        if (httpclient != null) {
            httpclient.getConnectionManager().shutdown();
        }
    }

    // return JSON String
    return rssFeed;

}

From source file:com.aikidonord.utils.JSONRequest.java

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {/*from  www. ja  v  a 2 s . co  m*/
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    //System.out.println("AIKIDONORD : " + json);
    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
        return null;
    }

    // return JSON String
    return jObj;

}

From source file:com.currencyconverter.model.WebService.java

public JSONObject getJson(String url) throws ClientProtocolException, IOException {

    JSONObject jObj = null;/*from   w  ww.  j  ava 2s .c o  m*/
    InputStream is = null;
    String json = "";

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + json + e.toString());
    }

    // return JSON String
    return jObj;
}

From source file:com.example.mysqltest.ServiceHandler.java

public String makeServiceCall(String url, int method, List<NameValuePair> params) {
    try {//from   ww  w.  ja v a 2  s .c o m

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);

            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {

            if (params != null) {
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        response = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error: " + e.toString());
    }

    return response;
}

From source file:com.sked.gdg.service.WebService.java

public String webGet(String webServiceUrl, String methodName, Map<String, String> params) {
    String getUrl = webServiceUrl + methodName;

    int i = 0;//from  w  w  w  .ja  va2  s  .c o  m
    if (null != params) {
        for (Map.Entry<String, String> param : params.entrySet()) {
            if (i == 0) {
                getUrl += "?";
            } else {
                getUrl += "&";
            }

            try {
                getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            i++;
        }

    }

    Log.d("getUrl", getUrl);
    httpGet = new HttpGet(getUrl);

    httpGet.setHeader("apikey", "85314139-B77A-4D1A-9692-2409663D5E6E");
    httpGet.setHeader("authtoken", "B198645B-DF03-4E23-874B-4651E8132E75");
    //Log.e("WebGetURL: ", getUrl);

    try {
        response = httpClient.execute(httpGet);
    } catch (Exception e) {
        Log.e("GDG:", e.toString());
    }

    // we assume that the response body contains the error message
    try {
        if (response != null) {
            if (response.getEntity() != null)
                ret = EntityUtils.toString(response.getEntity());
            Log.d("Response Code:", response.getStatusLine().getStatusCode() + "");
        }
    } catch (IOException e) {
        Log.e("GDG:", e.toString());
    }
    return ret;
}

From source file:com.fitforbusiness.webservice.WebService.java

public String webGet(String webServiceUrl, String methodName, Map<String, String> params) {
    String getUrl = webServiceUrl + methodName;

    int i = 0;/*from  w  w w.  j a v a2 s .  c  o m*/
    if (null != params) {
        for (Map.Entry<String, String> param : params.entrySet()) {
            if (i == 0) {
                getUrl += "?";
            } else {
                getUrl += "&";
            }

            try {
                getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            i++;
        }

    }

    Log.d("getUrl", getUrl);
    httpGet = new HttpGet(getUrl);

    httpGet.setHeader("apikey", "85314139-B77A-4D1A-9692-2409663D5E6E");
    httpGet.setHeader("authtoken", "B198645B-DF03-4E23-874B-4651E8132E75");
    //Log.e("WebGetURL: ", getUrl);

    try {
        response = httpClient.execute(httpGet);
    } catch (Exception e) {
        Log.e("FitnessApp:", e.toString());
    }

    // we assume that the response body contains the error message
    try {
        if (response != null) {
            if (response.getEntity() != null)
                ret = EntityUtils.toString(response.getEntity());
            Log.d("Response Code:", response.getStatusLine().getStatusCode() + "");
        }
    } catch (IOException e) {
        Log.e("FitnessApp:", e.toString());
    }
    return ret;
}

From source file:de.tudarmstadt.dvs.myhealthassistant.myhealthhub.commontools.JSONParser.java

public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {

    // Making HTTP request
    try {//w w w.  j a va2  s .c o  m

        // check for request method
        if (method == "POST") {
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } else if (method == "GET") {
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data: " + e.toString());
        Log.e("JSON Parser", "Error parsing data; source: " + json);
    }

    // return JSON String
    return jObj;

}