如今,手机应用渗透到各行各业,数量难以计数,其中大多数应用都会使用到网络,与服务器的交互势不可挡,那么android当中访问网络有哪些方式呢?
现总结了六种方式:
- 针对TCP/IP的Socket、ServerSocket
- 针对UDP的DatagramSocket、DatagramPackage。这里需要注意的是,考虑到Android设备通常是手持终端,IP都是随着上网进行分配的。不是固定的。因此开发也是有一点与普通互联网应用有所差异的。
- 针对直接URL的HttpURLConnection。
- Google集成了Apache HTTP客户端,可使用HTTP进行网络编程。
- 使用WebService。Android可以通过开源包如jackson去支持Xmlrpc和Jsonrpc,另外也可以用Ksoap2去实现Webservice。
- 直接使用WebView视图组件显示网页。基于WebView 进行开发,Google已经提供了一个基于chrome-lite的Web浏览器,直接就可以进行上网浏览网页。
1.socket与serverSocket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| public class TestNetworkActivity extends Activity implements OnClickListener{ private Button connectBtn; private Button sendBtn; private TextView showView; private EditText msgText; private Socket socket; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.test_network_main); connectBtn = (Button) findViewById(R.id.test_network_main_btn_connect); sendBtn = (Button) findViewById(R.id.test_network_main_btn_send); showView = (TextView) findViewById(R.id.test_network_main_tv_show); msgText = (EditText) findViewById(R.id.test_network_main_et_msg); connectBtn.setOnClickListener(this); sendBtn.setOnClickListener(this);
handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); String data = msg.getData().getString("msg"); showView.setText("来自服务器的消息:"+data); } }; } @Override public void onClick(View v) { if(v == connectBtn){ connectServer(); } if(v == sendBtn){ String msg = msgText.getText().toString(); send(msg); } }
public void connectServer(){ try { socket = new Socket("192.168.1.100",4000); System.out.println("连接服务器成功"); recevie(); } catch (Exception e) { System.out.println("连接服务器失败"+e); e.printStackTrace(); } }
public void send(String msg){ try { PrintStream ps = new PrintStream(socket.getOutputStream()); ps.println(msg); ps.flush(); } catch (IOException e) {
e.printStackTrace(); } }
public void recevie(){ new Thread(){ public void run(){ while(true){ try { InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = br.readLine(); Message message = new Message(); Bundle bundle = new Bundle(); bundle.putString("msg", str); message.setData(bundle); handler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace(); } } } }.start();
} }
|
2.URL、URLConnection、httpURLConnection、ApacheHttp、WebView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| public class TestURLActivity extends Activity implements OnClickListener { private Button connectBtn; private Button urlConnectionBtn; private Button httpUrlConnectionBtn; private Button httpClientBtn; private ImageView showImageView; private TextView showTextView; private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.test_url_main); connectBtn = (Button) findViewById(R.id.test_url_main_btn_connect); urlConnectionBtn = (Button) findViewById(R.id.test_url_main_btn_urlconnection); httpUrlConnectionBtn = (Button) findViewById(R.id.test_url_main_btn_httpurlconnection); httpClientBtn = (Button) findViewById(R.id.test_url_main_btn_httpclient); showImageView = (ImageView) findViewById(R.id.test_url_main_iv_show); showTextView = (TextView) findViewById(R.id.test_url_main_tv_show); webView = (WebView) findViewById(R.id.test_url_main_wv); connectBtn.setOnClickListener(this); urlConnectionBtn.setOnClickListener(this); httpUrlConnectionBtn.setOnClickListener(this); httpClientBtn.setOnClickListener(this); } @Override public void onClick(View v) { if (v == connectBtn) {
try { URL url = new URL("http://192.168.1.100:8080/myweb/image.jpg"); InputStream is = url.openStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); showImageView.setImageBitmap(bitmap); } catch (Exception e) {
e.printStackTrace(); } } if (v == urlConnectionBtn) { try {
URL url = new URL("http://192.168.1.100:8080/myweb/hello.jsp"); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); byte[] bs = new byte[1024]; int len = 0; StringBuffer sb = new StringBuffer(); while ((len = is.read(bs)) != -1) { String str = new String(bs, 0, len); sb.append(str); } showTextView.setText(sb.toString()); } catch (Exception e) {
e.printStackTrace(); } } if (v == httpUrlConnectionBtn) {
try { URL url = new URL( "http://192.168.1.100:8080/myweb/hello.jsp?username=abc"); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("GET"); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { String message = connection.getResponseMessage(); showTextView.setText(message); } } catch (Exception e) {
e.printStackTrace(); } } if (v == httpClientBtn) { try {
HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost( "http://192.168.1.100:8080/myweb/hello.jsp"); List dataList = new ArrayList(); dataList.add(new BasicNameValuePair("username", "aaaaa")); dataList.add(new BasicNameValuePair("pwd", "123")); httpPost.setEntity(new UrlEncodedFormEntity(dataList)); HttpResponse httpResponse = client.execute(httpPost); HttpEntity entity = httpResponse.getEntity(); String content = EntityUtils.toString(entity);
webView.loadDataWithBaseURL(null, content, "text/html", "utf-8", null); } catch (ClientProtocolException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); } } } }
|
3.使用webService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public class LoginActivity extends Activity implements OnClickListener{ private Button loginBtn; private static final String SERVICE_URL = "http://192.168.1.100:8080/loginservice/LoginServicePort"; private static final String NAMESPACE = "http://service.lovo.com/"; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.login_main); loginBtn = (Button) findViewById(R.id.login_main_btn_login); loginBtn.setOnClickListener(this); } @Override public void onClick(View v) {
if(v == loginBtn){ HttpTransportSE httpSE = new HttpTransportSE(SERVICE_URL); SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11); SoapObject soapObject = new SoapObject(NAMESPACE, "validate"); soapObject.addProperty("arg0", "abc"); soapObject.addProperty("arg1","123"); envelop.bodyOut = soapObject; try { httpSE.call(null, envelop); SoapObject resultObj = (SoapObject) envelop.bodyIn; String returnStr = resultObj.getProperty("return").toString(); Toast.makeText(this, "返回值:"+returnStr, Toast.LENGTH_LONG).show(); } catch (IOException e) {
e.printStackTrace(); } catch (XmlPullParserException e) {
e.printStackTrace(); } } } }
|