List of usage examples for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient
public DefaultHttpClient()
From source file:Main.java
public static String getRepsonseString(String url, Map<String, String> params) { HttpPost request = new HttpPost(url); List<NameValuePair> p = new ArrayList<NameValuePair>(); for (String key : params.keySet()) { p.add(new BasicNameValuePair(key, params.get(key))); }//from ww w .j a v a 2 s. c o m HttpClient httpClient = new DefaultHttpClient(); String result = null; try { request.setEntity(new UrlEncodedFormEntity(p, HTTP.UTF_8)); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == 200) { result = new String(EntityUtils.toString(response.getEntity()).getBytes("ISO_8859_1"), "UTF-8"); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (httpClient != null) httpClient.getConnectionManager().shutdown(); } return result; }
From source file:Main.java
public static String initializeService(String data, String serviceName, String serviceUrl) { Log.d("EliademyUtils", "initializeService"); try {//from w w w. ja va 2 s. co m JSONObject jsObj = new JSONObject(data.toString()); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(serviceUrl + "/login/token.php"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("username", jsObj.get("username").toString())); nameValuePairs.add(new BasicNameValuePair("password", jsObj.get("password").toString())); nameValuePairs.add(new BasicNameValuePair("service", serviceName)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); if (entity != null) { StringBuilder jsonStr = new StringBuilder(); InputStream iStream = entity.getContent(); BufferedReader bufferReader = new BufferedReader(new InputStreamReader(iStream)); String jpart; while ((jpart = bufferReader.readLine()) != null) { jsonStr.append(jpart); } iStream.close(); Log.d("Moodle", jsonStr.toString()); JSONObject jsonObj = new JSONObject(jsonStr.toString()); return (String) jsonObj.get("token"); } } catch (Exception e) { Log.e("EliademyUtils", "exception", e); return null; } return null; }
From source file:de.nico.asura.tools.JSONParser.java
public static JSONObject getJSONFromUrl(String url) { // Make HTTP request try {//from w ww . j av a2 s . co m DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { Log.e("UnsupportedEncodingException", e.toString()); } catch (ClientProtocolException e) { Log.e("ClientProtocolException", e.toString()); } catch (IOException e) { Log.e("IOException", e.toString()); } try { InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader reader = new BufferedReader(isr, 8); StringBuilder sb = new StringBuilder(); String line; 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()); } // return JSON String return jObj; }
From source file:com.devdungeon.httpexamples.DownloadFile.java
private static void download(String url, String filepath) { HttpClient client = new DefaultHttpClient(); HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 1000 * 5); HttpConnectionParams.setSoTimeout(params, 1000 * 5); HttpGet request = new HttpGet(url); HttpResponse response = null;/*from w ww.j av a 2 s .com*/ try { response = client.execute(request); } catch (IOException ex) { Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex); } BufferedReader rd; String line = null; try { rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); FileWriter fileWriter = new FileWriter(filepath); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); while ((line = rd.readLine()) != null) { bufferedWriter.write(line); bufferedWriter.newLine(); } bufferedWriter.close(); } catch (IOException ex) { Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedOperationException ex) { Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:Main.java
public static String openGetConnection(String urlString, Map<String, String> map) { List<NameValuePair> nameValuePairs = new ArrayList<>(); for (String key : map.keySet()) { nameValuePairs.add(new BasicNameValuePair(key, map.get(key))); }// w w w . j ava2 s . c o m String params = URLEncodedUtils.format(nameValuePairs, "utf-8"); urlString += "?"; urlString += params; HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(urlString); try { HttpResponse httpResponse = client.execute(get); BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); StringBuilder json = new StringBuilder(""); String line = null; while ((line = br.readLine()) != null) { json.append(line); } return json.toString(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Generate an input stream reading from an android URI * /*from w w w .j av a 2 s .c o m*/ * @param uri * @return * @throws IOException */ public static InputStream getFromURI(Context context, Uri uri) throws IOException { if (uri.getScheme().equals("content")) return context.getContentResolver().openInputStream(uri); else if (uri.getScheme().equals("file")) { URL url = new URL(uri.toString()); return url.openStream(); } else { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(uri.toString()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) return entity.getContent(); else throw new IOException("No HTTP response"); // Use the regular java stuff // URL url = new URL(uri.toString()); // return url.openStream(); } }
From source file:it.polimi.brusamentoceruti.moviebookrest.boundary.JsonRequest.java
public static JSONObject doQuery(String Url) throws JSONException, IOException { String responseBody = null;//from w w w . j av a2s. com HttpGet httpget; HttpClient httpClient = new DefaultHttpClient(); try { httpget = new HttpGet(Url); } catch (IllegalArgumentException iae) { return null; } HttpResponse response = httpClient.execute(httpget); InputStream contentStream = null; try { StatusLine statusLine = response.getStatusLine(); if (statusLine == null) { throw new IOException(String.format("Unable to get a response from server")); } int statusCode = statusLine.getStatusCode(); if (statusCode < 200 && statusCode >= 300) { throw new IOException( String.format("OWM server responded with status code %d: %s", statusCode, statusLine)); } /* Read the response content */ HttpEntity responseEntity = response.getEntity(); contentStream = responseEntity.getContent(); Reader isReader = new InputStreamReader(contentStream); int contentSize = (int) responseEntity.getContentLength(); if (contentSize < 0) contentSize = 8 * 1024; StringWriter strWriter = new StringWriter(contentSize); char[] buffer = new char[8 * 1024]; int n = 0; while ((n = isReader.read(buffer)) != -1) { strWriter.write(buffer, 0, n); } responseBody = strWriter.toString(); contentStream.close(); } catch (IOException e) { throw e; } catch (RuntimeException re) { httpget.abort(); throw re; } finally { if (contentStream != null) contentStream.close(); } return new JSONObject(responseBody); }
From source file:it410.gmu.edu.OrderingServiceClient.java
public static void addCustomerJSON(Customer customer) throws IOException { Gson gson = new Gson(); String customerString = gson.toJson(customer); System.out.println("Customer JSON = " + customerString); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://localhost:8080/BookstoreRestService/generic/addOrderJSON"); post.setHeader("Content-Type", "application/json"); StringEntity entity = new StringEntity(customerString); post.setEntity(entity);//from w ww .j av a2s .co m HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); } }
From source file:Main.java
public static String makeRequest55(String url, String json) { Log.v(TAG, "URL-->" + url); Log.v(TAG, "input-->" + json); try {/*from w w w. j ava 2s . co m*/ HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(json)); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); //text/html HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost); // receive response as inputStream InputStream inputStream = httpResponse.getEntity().getContent(); // convert inputstream to string if (inputStream != null) { String result = convertInputStreamToString(inputStream); Log.v(TAG, "output-->" + result); return result; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; }
From source file:Main.java
public static String makeRequest3(String url, String json) { Log.v(TAG, "URL-->" + url); Log.v(TAG, "input-->" + json); try {//ww w . jav a 2 s .c o m HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(json)); //httpPost.setHeader("sessionToken",AuthToken); // httpPost.setHeader("Accept", "application/json"); // httpPost.setHeader("Content-type", "application/json"); //text/html HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost); // receive response as inputStream InputStream inputStream = httpResponse.getEntity().getContent(); // convert inputstream to string if (inputStream != null) { String result = convertInputStreamToString(inputStream); Log.d("tag", "outputtttttttttt-->" + result); return result; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; }