Back to project page adr.
The source code is released under:
Copyright (c) 2014, blake kim All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Re...
If you think the Android project adr listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.kkh.httptest; //from ww w .j av a2s . c om import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.nio.ByteBuffer; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { static final String tag = "http"; HttpURLConnection mCnn; ComTask mComTask; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e(tag, "onCre..."); initHttp(); } void initHttp() { mComTask = new ComTask(); mComTask.execute(null, null, null); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private class ComTask extends AsyncTask<Void, Void, Integer> { @Override protected Integer doInBackground(Void... params) { try { URL url = new URL("http://www.google.co.kr"); mCnn = (HttpURLConnection) url.openConnection(); mCnn.connect(); if (mCnn.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(mCnn.getInputStream(), "UTF-8")); String s; for (;;) { s = reader.readLine(); if(s != null) Log.d(tag, "recv line=" + s); else break; } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(Integer result) { super.onPostExecute(result); } } }