編輯:關於Android編程
1、檢測網絡狀態的代碼
ConnectivityManager cm = (ConnectivityManager) Context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActivityNetworkInfo();
netInfo.toString();
2.使用網絡權限
<uses-permission android:name="android.permission.INTERNET"/>
3.通過URL + HttpURLConnection獲得數據(文本和圖片)
URL url = new URL("http://www.sohu.com");
HttpURLConnection conn = (HttpURLConnectioni) url.openConnection();
conn.setConnectionTimeout(5*1000);
conn.setRequestMethod("GET");
conn.getResponseMethod("GET");
(conn.getResponseCode() != 200 ) throw...;
InputStream is = conn.getInputStream();
String result = readData(is,"GEK");
conn.disconnect();
//獲取圖片同上
1.GET方式發送name-value對
//http://xxxx/xxx.action?name=tom&&age=12
URL realUrl = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectionTimeout(5000);
//直到此時,數據才發往服務器
if( conn.getResponseCode() == 200 ){
String result = readAsString(conn.getInputStream(), "UTF-8");
outStream.close();
System.out.println(result);
}
注:中文亂碼問題.
//utf-8指服務器端編碼
1.get方式發送中文需要轉碼.UrlEncode("中文","utf-8");
2.tomcat器端需要轉碼(get方式).
if("GET".equals(request.getMethod())){
String v =request.getParameter("name");
new String(v.getBytes("ISO8859-1"),"UTF-8");
}
1.POST發送普通文本內容
URL realUrl = new URL(requestUrl);
HttpURLConncection conn = (HttpURLConnection) realUrl.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setUseCaches(false);
conn.setRequestMethod();
conn.setRequestProperty("Connection","Keep-Alive");//維持長連接
conn.setRequestProperty("Charset","UTF-8");
con.setRequestProperty("Content-Length", String.valueOf(data.length));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
String str = "name=xxx&age=12";
con.getOutputStream().write(str.getBytes);
If(conn.getOutputStream().write(Str.getBytes)){
if(conn.getResponseCode()==200){
String result = readAsString(conn.getInputStream(),"UTF-8");
outStream.close();
System.out.println(result):
}
}
1.POST發送xml
StringBuilder xml = new StringBuilder();
xml.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
xml.append("<M1 V=10000>");
xml.append("<U I=1 D=\"N73\">中國</U>");
xml.append("</M1>");
byte[] xmlbyte = xml.toString().getBytes("UTF-8");
URL url = new URL("http://xxx.do?method=readxml");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5* 1000);
conn.setDoOutput(true);//允許輸出
conn.setUseCaches(false);//不使用Cache
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");//維持長連接
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(xmlbyte.length));
//必須設置內容類型,否則服務器無法識別數據類型.www.2cto.com
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(xmlbyte);//發送xml數據
outStream.flush();
作者:toto1297488504
准備需要下載ntfs-3g驅動包,並做相應修改,這個網上已經可以下載到修改好的包,本文最後也會附加。為什麼要移植在Android原生代碼中,只支持了FAT格式的掛載,並未
本文實例講述了Android編程實現二級下拉菜單及快速搜索的方法。分享給大家供大家參考,具體如下:一、我們要做什麼?上面有個搜索框,下面是一個二級下拉菜單。輸入查詢內容,
之前用Linux Deploy 部署了Kali Linux 。讓我這陣子拿到平板有一半的時間是在用終端模擬器(Terminal Emulator)連接。安卓的終端模擬器,
我們在上一篇博客《Android UI設計——ViewPage中PagerTabStrip與PagerTitleStrip添加標題欄(三)》 中學