Вы находитесь на странице: 1из 4

Android HTTP Access - Tutorial

http://www.vogella.com/articles/AndroidNetworking/article.html

104

java.net

java.net

HttpURLConnection HttpURLConnection

1 of 4

6/1/2013 1:26 PM

Android HTTP Access - Tutorial

http://www.vogella.com/articles/AndroidNetworking/article.html

android.permission.ACCESS_NETWORK_STATE

StrictMode StrictMode NetworkOnMainThreadException

onCreate()
StrictMode.ThreadPolicy policy = new StrictMode. ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);

HttpURLConnection

HttpURLConnection Accept-Encoding: gzip

// Somewhere in your code this is called // in a thread which is not the user interface // thread
try { URL url = new URL("http://www.vogella.com"); HttpURLConnection con = (HttpURLConnection) url .openConnection(); readStream(con.getInputStream()); } catch (Exception e) { e.printStackTrace(); }

private void readStream(InputStream in) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) {

2 of 4

6/1/2013 1:26 PM

Android HTTP Access - Tutorial

http://www.vogella.com/articles/AndroidNetworking/article.html

if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }

HttpURLConnection HttpURLConnection Threads

HttpURLConnection

public boolean isNetworkAvailable() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo();

// if no network is available networkInfo will be null // otherwise check if we are connected


if (networkInfo != null && networkInfo.isConnected()) { return true; } return false; }

ACCESS_NETWORK_STATE

Settings onCreate
Settings.System.putString(getContentResolver(), Settings.System.HTTP_PROXY, "myproxy:8080");

android.permission.WRITE_SETTINGS
AndroidManifest.xml

3 of 4

6/1/2013 1:26 PM

Android HTTP Access - Tutorial

http://www.vogella.com/articles/AndroidNetworking/article.html

4 of 4

6/1/2013 1:26 PM

Вам также может понравиться