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:com.phonty.improved.DirectionCost.java

public String get(String phone) {
    StringBuilder builder = new StringBuilder();
    String value = "0";

    client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Phonty-Android-Client");
    httppost = new HttpPost(APIURL);
    httppost.addHeader("Content-Type", "application/json; charset=\"utf-8\"");

    try {/*  w ww. j  a  v a2 s . com*/
        String locale = context.getResources().getConfiguration().locale.getCountry();
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("phone", phone));
        nvps.add(new BasicNameValuePair("locale", locale));

        httppost.setEntity(new UrlEncodedFormEntity(nvps));

        HttpResponse response = client.execute(httppost);

        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) {
                builder.append(line);
                value = Parse(line);
                this.VALUE = value;
            }
        } else {
            this.VALUE = "0.0";
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        this.VALUE = "Protocol exception";
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        this.VALUE = "I.O. Exception";
        e.printStackTrace();
    }

    return value;
}

From source file:ru.neverdark.yotta.parser.YottaParser.java

private void parse(Array array) {
    final String URL = String.format("http://%s/hierarch.htm", array.getIp());
    final StringBuffer result = new StringBuffer();

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(array.getIp(), 80),
            new UsernamePasswordCredentials(array.getUser(), array.getPassword()));
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {/*from   w  w  w  . j  a v  a  2 s .  com*/
        HttpGet httpget = new HttpGet(URL);
        CloseableHttpResponse response = httpClient.execute(httpget);
        System.err.printf("%s\t%s\n", array.getIp(), response.getStatusLine());
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }

            Document doc = Jsoup.parse(result.toString());
            Elements tables = doc.getElementsByAttribute("vspace");
            // skip first
            for (int i = 1; i < tables.size(); i++) {
                parseTable(tables.get(i), array.getType());
            }

        } finally {
            response.close();
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            httpClient.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

From source file:com.viettel.viettellib.json.me.JSONParser.java

/**
 * /*  ww  w.  j ava2 s .  c o m*/
*  Mo ta chuc nang cua ham
*  @author: BangHN
*  @param address 
*  @param method
*  @return
*  @throws URISyntaxException
*  @return: json string
*  @throws:
 */
public String excuteHttpRequest(String address, String method) {
    String jsonStr = "";

    String url = null;
    //Tao duong dan truy cap
    try {
        URI uri = new URI("http", "maps.googleapis.com", "/maps/api/geocode/json",
                "address=" + address + "&sensor=true", null);
        url = uri.toASCIIString();
    } catch (URISyntaxException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //1.truy vn server http://10.0.0.103/ltjoomla2106/index.php?option=com_json&format=json&id=15&task=requestdb
    /*to 1 client: chc nng ging nh trnh duyt*/
    DefaultHttpClient client = new DefaultHttpClient();

    /*to bin post  y d liu: --> to header ca giao thc http*/
    if (method == "POST") {
        HttpPost postObj = new HttpPost(url);
        /*t cc gi tr cn post vo bin http header*/
        try {
            //ly v response l d liu chui JSON
            /*bm nt submit form trn trnh duyt*/
            HttpResponse jsonString = client.execute(postObj);

            jsonStr = EntityUtils.toString(jsonString.getEntity());

            Log.e("JSON STRiNG", jsonStr);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (method == "GET") {
        HttpGet getObj = new HttpGet(url);
        /*t cc gi tr cn post vo bin http header*/
        try {
            //ly v response l d liu chui JSON
            /*bm nt submit form trn trnh duyt*/
            HttpResponse jsonString = client.execute(getObj);

            jsonStr = EntityUtils.toString(jsonString.getEntity());

            Log.e("JSON STRiNG", jsonStr);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return jsonStr;
}

From source file:net.liuxuan.Tools.signup.SignupV2ex.java

/**
 * ?? get/*from   ww  w  .  j  ava  2  s . com*/
 */
public String get(String url) {
    //httpclient = new DefaultHttpClient();
    String resStr = "";
    try {
        // httpget.
        HttpGet httpget = new HttpGet(url);
        // get.
        HttpResponse response = httpclient.execute(httpget);
        // ??
        HttpEntity entity = response.getEntity();
        // ???
        System.out.println(response.getStatusLine());
        if (entity != null) {
            // ??
            //                System.out.println("Response content length: "
            //                        + entity.getContentLength());
            // ??
            //                System.out.println("Response content: "
            //                        + EntityUtils.toString(entity));
            resStr = EntityUtils.toString(entity);
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // ,?
        //httpclient.getConnectionManager().shutdown();
    }
    return resStr;
}

From source file:es.uma.lcc.tasks.PictureDetailsTask.java

@Override
public Void doInBackground(Void... args) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    int width, height;
    final HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, false);
    httpclient.setParams(params);//  w ww .j  a va  2s.  co m
    String target = SERVERURL + "?" + QUERYSTRING_ACTION + "=" + ACTION_PICTUREDETAILS + "&"
            + QUERYSTRING_PICTUREID + "=" + mPicId;
    HttpGet httpget = new HttpGet(target);
    while (mCookie == null)
        mCookie = mMainActivity.getCurrentCookie();

    httpget.setHeader("Cookie", mCookie.getName() + "=" + mMainActivity.getCurrentCookie().getValue());
    try {
        HttpResponse response = httpclient.execute(httpget);

        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != 200) {
            throw new IOException("Invalid response from server: " + status.toString());
        }

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = entity.getContent();
            ByteArrayOutputStream content = new ByteArrayOutputStream();

            // Read response into a buffered stream
            int readBytes = 0;
            byte[] sBuffer = new byte[256];
            while ((readBytes = inputStream.read(sBuffer)) != -1) {
                content.write(sBuffer, 0, readBytes);
            }
            String result = new String(content.toByteArray());

            try {
                JSONArray jsonArray = new JSONArray(result);
                if (jsonArray.length() == 0) {
                    // should never happen
                    Log.e(APP_TAG, LOG_ERROR + ": Malformed response from server");
                } else {
                    // Elements in a JSONArray keep their order
                    JSONObject successState = jsonArray.getJSONObject(0);
                    if (successState.get(JSON_RESULT).equals(JSON_RESULT_ERROR)) {
                        if (successState.getBoolean(JSON_ISAUTHERROR) && mIsFirstRun) {
                            mIsAuthError = true;
                        } else {
                            Log.e(APP_TAG,
                                    LOG_ERROR + ": Server found an error: " + successState.get(JSON_REASON));
                        }
                    } else {
                        ArrayList<String> users = new ArrayList<String>();
                        ArrayList<String> coords = new ArrayList<String>();
                        ArrayList<String> ids = new ArrayList<String>();
                        JSONObject obj = jsonArray.getJSONObject(0);
                        width = obj.getInt(JSON_IMGWIDTH);
                        height = obj.getInt(JSON_IMGHEIGHT);
                        for (int i = 1; i < jsonArray.length(); i++) {
                            obj = jsonArray.getJSONObject(i);
                            users.add(obj.getString(JSON_USERNAME));
                            coords.add(formatCoordinates(obj.getInt(JSON_HSTART), obj.getInt(JSON_HEND),
                                    obj.getInt(JSON_VSTART), obj.getInt(JSON_VEND)));
                            ids.add(obj.getString(JSON_PERMISSIONID));
                        }
                        Intent intent = new Intent(mMainActivity, PictureDetailsActivity.class);
                        intent.putStringArrayListExtra("users", users);
                        intent.putStringArrayListExtra("coordinates", coords);
                        intent.putStringArrayListExtra("ids", ids);
                        intent.putExtra("height", height);
                        intent.putExtra("width", width);
                        intent.putExtra("picId", mPicId);
                        intent.putExtra("username", mMainActivity.getUserEmail());
                        mMainActivity.startActivityForResult(intent, ACTIVITY_PICTURE_DETAILS);
                    }
                }
            } catch (JSONException jsonEx) {
                Log.e(APP_TAG, LOG_ERROR + ": Malformed JSON response from server");
            }
        } else { // entity is null
            Log.e(APP_TAG, LOG_ERROR + ": null response from server");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:at.fhooe.mcm.webdav.WebDavInterface.java

/**
 * executes a http post request/*w  w w.  j ava 2s .  c om*/
 * 
 * also adds the user/password to the request params
 * 
 * @param values
 * @return
 */
private RestResponse doRequest(List<NameValuePair> values) {
    HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpPost post = new HttpPost(url);

    // Execute the request
    String data = "";
    int returnCode = -1;
    try {
        values.add(new BasicNameValuePair("user", user));
        values.add(new BasicNameValuePair("pw", pw));

        HttpEntity entity = new UrlEncodedFormEntity(values);
        post.setEntity(entity);
        HttpResponse response = httpclient.execute(post);
        // Examine the response status

        returnCode = response.getStatusLine().getStatusCode();
        data = convertStreamToString(response.getEntity().getContent());

        return new RestResponse(returnCode, data);

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        data = e.getClass().getName() + " " + e.getMessage();
    } catch (IOException e) {
        e.printStackTrace();
        data = e.getClass().getName() + " " + e.getMessage();
    }

    return new RestResponse(-1, data);
}

From source file:net.seedboxer.seedroid.utils.RestClient.java

private void executeRequest(HttpUriRequest request, String url) {
    DefaultHttpClient client = new DefaultHttpClient();
    if (credProvider != null) {
        client.setCredentialsProvider(credProvider);
    }/*from  ww  w  .j  a  va2s.  c  o m*/

    HttpResponse httpResponse;

    try {
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStreamToString(instream);

            // Closing the input stream will trigger connection release
            instream.close();
        }

    } catch (ClientProtocolException e) {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    } catch (IOException e) {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    }
}

From source file:org.megam.deccanplato.provider.box.handler.FileImpl.java

/**
 * @return/*from   w  ww.  ja  v a  2s.  c  om*/
 */
private Map<String, String> delete() {

    Map<String, String> outMap = new HashMap<>();
    final String BOX_UPLOAD = "/files/" + args.get(FILE_ID);

    Map<String, String> headerMap = new HashMap<String, String>();
    headerMap.put("Authorization", "BoxAuth api_key=" + args.get(API_KEY) + "&auth_token=" + args.get(TOKEN));
    headerMap.put("If-Match", args.get(ETAG));

    TransportTools tools = new TransportTools(BOX_URI + BOX_UPLOAD, null, headerMap);

    String responseBody = "";
    TransportResponse response = null;
    try {
        response = TransportMachinery.delete(tools);
        responseBody = response.entityToString();
        System.out.println("OUTPUT:" + responseBody);
    } catch (ClientProtocolException ce) {
        ce.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    outMap.put(OUTPUT, responseBody);
    return outMap;
}

From source file:org.megam.deccanplato.provider.box.handler.FileImpl.java

/**
 * @return//from  ww w.  ja  v  a 2s. c o  m
 */
private Map<String, String> view() {
    Map<String, String> outMap = new HashMap<>();

    final String BOX_DOWNLOAD = "/files/" + args.get(FILE_ID);

    Map<String, String> headerMap = new HashMap<String, String>();
    headerMap.put("Authorization", "BoxAuth api_key=" + args.get(API_KEY) + "&auth_token=" + args.get(TOKEN));

    TransportTools tools = new TransportTools(BOX_URI + BOX_DOWNLOAD, null, headerMap);

    String responseBody = null;
    TransportResponse response = null;
    try {
        response = TransportMachinery.get(tools);
        responseBody = response.entityToString();
        System.out.println("OUTPUT:" + responseBody);
    } catch (ClientProtocolException ce) {
        ce.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    outMap.put(OUTPUT, responseBody);
    return outMap;
}

From source file:org.megam.deccanplato.provider.box.handler.FileImpl.java

/**
 * @return/*from w w w . ja v  a 2s  . co m*/
 */
private Map<String, String> download() {
    System.out.println("File Download");
    Map<String, String> outMap = new HashMap<>();

    final String BOX_DOWNLOAD = "/files/" + args.get(FILE_ID) + "." + args.get(FILE_TYPE) + "/content";

    Map<String, String> headerMap = new HashMap<String, String>();
    headerMap.put("Authorization", "BoxAuth api_key=" + args.get(API_KEY) + "&auth_token=" + args.get(TOKEN));

    TransportTools tools = new TransportTools(BOX_URI + BOX_DOWNLOAD, null, headerMap);

    String responseBody = null;
    TransportResponse response = null;
    try {
        response = TransportMachinery.get(tools);
        responseBody = response.entityToString();
        System.out.println("OUTPUT:" + responseBody);
    } catch (ClientProtocolException ce) {
        ce.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    outMap.put(OUTPUT, responseBody);
    return outMap;
}