DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); HttpPost httppost = new HttpPost(http://someJSONUrl/jsonWebService); // Depends on your web service httppost.setHeader("Content-type", "application/json"); InputStream inputStream = null; String result = null; try { HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); inputStream = entity.getContent(); // json is UTF-8 by default BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } result = sb.toString(); } catch (Exception e) { // Oops } finally { try{if(inputStream != null)inputStream.close();}catch(Exception squish){} }
protected HttpClient createHttpClient(GoogleAnalyticsConfig config) { ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(); connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config)); BasicHttpParams params = new BasicHttpParams(); if (isNotEmpty(config.getUserAgent())) { params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent()); } if (isNotEmpty(config.getProxyHost())) { params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(config.getProxyHost(), config.getProxyPort())); } DefaultHttpClient client = new DefaultHttpClient(connManager, params); if (isNotEmpty(config.getProxyUserName())) { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()), new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword())); client.setCredentialsProvider(credentialsProvider); } return client; }
/** * Creates a copy of these parameters. * The implementation here instantiates {@link BasicHttpParams}, * then calls {@link #copyParams(HttpParams)} to populate the copy. * * @return a new set of params holding a copy of the * <i>local</i> parameters in this object. */ public HttpParams copy() { BasicHttpParams clone = new BasicHttpParams(); copyParams(clone); return clone; }
final HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is // established. HttpConnectionParams.setConnectionTimeout(httpParameters, 7000); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. HttpConnectionParams.setSoTimeout(httpParameters, 10000); HttpClient client = new DefaultHttpClient(httpParameters); HttpResponse response = client.execute(new HttpGet(path)); HttpEntity entity = response.getEntity(); InputStream imageContentInputStream = entity.getContent();
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); DefaultHttpClient defaultClient = new DefaultHttpClient(); // Setup the get request HttpGet httpGetRequest = new HttpGet("http://10.0.2.2:8080"); try{ // Execute the request in the client HttpResponse httpResponse = defaultClient.execute(httpGetRequest); // Grab the response BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8")); json = reader.readLine(); }catch(Exception e){ Log.d("Error",e.toString()); }
HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schReg.register(new Scheme("https", socketFactory, 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); DefaultHttpClient sClient = new DefaultHttpClient(conMgr, params); HttpResponse response = sClient.execute(httpGet); HttpEntity httpEntity = response.getEntity(); InputStream is = httpEntity.getContent(); BufferedReader read = new BufferedReader(new InputStreamReader(is)); String query = null; while ((query = read.readLine()) != null)
HttpParams httpParameters = new BasicHttpParams(); HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8); HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8); client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); client.getParams().setParameter("http.socket.timeout", new Integer(2000)); client.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8); httpParameters.setBooleanParameter("http.protocol.expect-continue", false); HttpPost request = new HttpPost("http://www.server.com/some_script.php?sid=" + String.valueOf(Math.random())); request.getParams().setParameter("http.socket.timeout", new Integer(5000)); request.setEntity(formEntity); HttpResponse response = client.execute(request); in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = "";
HttpPost httppost = new HttpPost( "http://www.somepage.php" ); HttpParams mHttpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(mHttpParams, myConnectionTimeOut ); HttpConnectionParams.setSoTimeout(mHttpParams, myTimeOut); HttpClient httpclient = new DefaultHttpClient( mHttpParams ); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); BufferedReader rd = new BufferedReader(new InputStreamReader( entity.getContent() )); StringBuilder sb = new StringBuilder(); String line = null; while ((line = rd.readLine()) != null) { sb.append(line ); } JSONArray jsonArray = json.getJSONArray("scores"); for( int i = 0; i < jsonArray .length(); i++ ) { /* Do what you want here*/ }
InputStream is = null; String result = ""; JSONObject jsonResult = null; HttpPost httppost = new HttpPost(url); //httppost.addHeader("content-type", "application/json"); //httppost.addHeader("User-Agent", userAgent); HttpParams httpParams = new BasicHttpParams(); int timeOutConnection = 5000; HttpConnectionParams.setConnectionTimeout(httpParams, timeOutConnection); int timeoutSocket = 5000; HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket); HttpClient httpclient = new DefaultHttpClient(httpParams); httppost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); jsonResult = new JSONObject(result);
private void PostData(MultipartEntity entity) { HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 20000); HttpConnectionParams.setSoTimeout(httpParameters, 20000); DefaultHttpClient client = new DefaultHttpClient(httpParameters); HttpPost request = new HttpPost("http://www.you_server_url.com/someFileName.php"); request.setEntity(entity); HttpResponse response = null; response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } // // Process the returned result from server... // }
try { HttpResponse response = null; HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 20000); HttpConnectionParams.setSoTimeout(httpParameters, 20000); HttpClient client = new DefaultHttpClient(httpParameters); HttpGet request = new HttpGet( "http://api.fitbit.com/oauth/request_token?oauth_consumer_key=7af733f021f649bcac32f6f7a4fe2e9a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1358921319&oauth_nonce=456236281&oauth_callback=http://www.androidhub4you.com/&oauth_version=1.0&oauth_token=5cefb18d2a80073520211f03f8d75321&oauth_signature=QdVUzMvT6tveGyoPu%2BEevzvo07s%3D"); response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader( response.getEntity().getContent())); String webServiceInfo = ""; while ((webServiceInfo = rd.readLine()) != null) { Log.e("****Step 1***", "Webservice: " + webServiceInfo); authToken = webServiceInfo.substring(12, 44); Log.e("Auth token:", "Webservice: " + authToken); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }
public void postData(String url,JSONObject obj) { // Create a new HttpClient and Post Header HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, 10000); HttpConnectionParams.setSoTimeout(myParams, 10000); HttpClient httpclient = new DefaultHttpClient(myParams ); String json=obj.toString(); try { HttpPost httppost = new HttpPost(url.toString()); httppost.setHeader("Content-type", "application/json"); StringEntity se = new StringEntity(obj.toString()); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); String temp = EntityUtils.toString(response.getEntity()); Log.i("tag", temp); } catch (ClientProtocolException e) { } catch (IOException e) { } }
try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("your url only ex:www.google.com/abc"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpParams httpParameters = new BasicHttpParams(); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("Loading Runnable Error in http connection :", e.toString()); }
nameValuePairs.add(new BasicNameValuePair("location", arg0[1])); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(arg0[0]); HttpParams httpParameters = new BasicHttpParams(); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); response = httpclient.execute(httppost); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase());
HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); HttpClient client = new DefaultHttpClient(httpParams); HttpPost request = new HttpPost( "http://www.colorssoftware.com/savedata.php"); request.setHeader( "http://10.0.2.2/projet/connection.php" ); JSONObject json = new JSONObject(); json.put("user_name", "username"); json.put("user_password", "password"); Log.i("jason Object", json.toString()); System.out.println("jason Object value is" + json.toString()); TextView text = (TextView)findViewById(R.id.textView); text.setText(json.toString()); StringEntity se = new StringEntity(json.toString()); se.setContentEncoding("UTF-8"); se.setContentType("application/json"); request.setEntity(se); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); String _response = convertStreamToString(is); int res_code = response.getStatusLine().getStatusCode();
final ClientConfiguration config) throws IOException { HttpParams httpClientParams = new BasicHttpParams(); HttpProtocolParams.setUserAgent( httpClientParams, getUserAgent(config, null)); httpClientParams, getSocketTimeout(config)); DefaultHttpClient httpclient = new DefaultHttpClient(httpClientParams); httpclient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy); && config.getProxyPassword() != null) { httpclient.getCredentialsProvider().setCredentials( new AuthScope(proxyHost, proxyPort), new NTCredentials(config.getProxyUsername(), HttpResponse response = httpclient.execute(new HttpGet(uri)); if (response.getStatusLine().getStatusCode() != 200) { throw new IOException("Error fetching file from " + uri + ": " + response); response.getEntity().getContent());
HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); HttpClient client = new DefaultHttpClient(httpParams); String url = "http://yourserver.com/script.php"; HttpPost request = new HttpPost(url); request.setEntity(new ByteArrayEntity(json_string.getBytes("UTF8"))); request.setHeader("json", json_string); HttpResponse response = client.execute(request); Log.d("FISHY", response.getStatusLine().toString());
byte[] data = null; HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(uploadPhotoUrl); HttpParams mHttpParams = new BasicHttpParams(); HttpConnectionParams.setSoTimeout(mHttpParams, 150000); httpPost.setParams(mHttpParams); MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 90, bos); data = bos.toByteArray(); entity.addPart("somefile", new ByteArrayBody(data, "temp.png")); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream instream = response.getEntity().getContent();
url = syncURL() + method; org.apache.http.client.methods.HttpPost httpPost = new org.apache.http.client.methods.HttpPost(url); org.apache.http.HttpEntity entity = new ProgressByteEntity(tmpFileBuffer); httpPost.setEntity(entity); httpPost.setHeader("Content-type", "multipart/form-data; boundary=" + BOUNDARY); org.apache.http.params.HttpParams params = new org.apache.http.params.BasicHttpParams(); params.setParameter(org.apache.http.conn.params.ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); params.setParameter(org.apache.http.conn.params.ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new org.apache.http.conn.params.ConnPerRouteBean(30)); org.apache.http.conn.scheme.SchemeRegistry registry = new org.apache.http.conn.scheme.SchemeRegistry(); registry.register(new org.apache.http.conn.scheme.Scheme("http", org.apache.http.conn.scheme.PlainSocketFactory.getSocketFactory(), 80)); registry.register(new org.apache.http.conn.scheme.Scheme("https", new EasySSLSocketFactory(), 443)); org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager cm = new org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager(params, registry); org.apache.http.client.HttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient(cm, params); org.apache.http.HttpResponse httpResponse = httpClient.execute(httpPost);
HttpParams params = new BasicHttpParams(); //this how tiny it might seems, is actually absoluty needed. otherwise http client lags for 2sec. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpClient httpClient = new DefaultHttpClient(params); HttpResponse httpResponse; HttpPost httpPost = new HttpPost("http://"+server+":"+port+"/"); StringEntity entity = new StringEntity(content, "utf-8"); entity.setContentType("text/plain; charset=utf-8"); httpPost.setEntity(entity); httpResponse=httpClient.execute(httpPost); String response = IOUtils.toString(httpResponse.getEntity().getContent(),encoding); httpResponse.getEntity().consumeContent(); httpClient.getConnectionManager().shutdown(); return(response);