1、WebView的簡單使用
安卓程序有的需要瀏覽網頁,因此android提供了一個控件叫WebView,話不多說,直接使用
界面佈局代碼,就顯示一個WebView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<WebView
android:id="@+id/main_show_webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
處理的代碼
public class MainActivity extends Activity {
private WebView mShowWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
//設置隱藏標題
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowWebView=(WebView) findViewById(R.id.main_show_webView);
//通過getSettings進行設置,然後設置支持javaScript
mShowWebView.getSettings().setJavaScriptEnabled(true);
mShowWebView.setWebViewClient(new WebViewClient(){
@Override
//該方法表明如果從當前網頁跳轉到別的網頁,還是在當前的webView中顯示,不使用系統的瀏覽器
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
//加載頁面
mShowWebView.loadUrl("http://www.baidu.com");
}
}
注意設置網絡訪問權限
2、HttpURLConnection的簡單使用
寫一個小例子,向百度發送請求,然後得到請求結果
界面佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/second_sendButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sendRequestWithHttpURLConnection"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/second_responseTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
</LinearLayout>
處理代碼(解釋見代碼註釋)
public class SecondActivity extends Activity {
private Button mSendButton;
private TextView mShowTextView;
private Handler mHandler;
private static final String TAG="SecondActivity";
private static final int SEND_REQUEST=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mSendButton=(Button) findViewById(R.id.second_sendButton);
mShowTextView=(TextView) findViewById(R.id.second_responseTextView);
//通過handler接收到從網頁獲取到的數據,在主線程進行更新
mHandler=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==SEND_REQUEST){
StringBuilder result=(StringBuilder) msg.obj;
mShowTextView.setText(result);
}
};
};
mSendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendRequestWithHttpURLConection();
}
});
}
private void sendRequestWithHttpURLConection(){
//由於訪問網絡是耗時操作,因此開一個子線程
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection=null;
try {
URL url=new URL("http://www.baidu.com");
//通過給定的url獲得connection
connection=(HttpURLConnection) url.openConnection();
//設置請求方法,GET表明從獲得數據
connection.setRequestMethod("GET");
//設置超時操作
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//得到輸入流
InputStream in=connection.getInputStream();
//通過輸入流讀取數據
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
StringBuilder content=new StringBuilder();
String line="";
while((line=reader.readLine())!=null){
content.append(line);
}
//將得到的結果通過handler發送到主線程,在主線程進行UI操作
Message msg=mHandler.obtainMessage(SEND_REQUEST,content);
mHandler.sendMessage(msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
3、HttpClient
HttpClient是Apche提供的HTTP網絡訪問接口,使用比較簡單,因爲他對一些參數都進行了封裝,解釋見代碼註釋
界面代碼略
private void sendRequestWithHttpClient() {
new Thread(new Runnable() {
@Override
public void run() {
try {
//HttpClient是一個接口,因此一般new DefaultHttpClient
HttpClient client=new DefaultHttpClient();
//獲得HttpClient
HttpGet get=new HttpGet("www.baidu.com");
//執行get請求,得到responce
HttpResponse response=client.execute(get);
//判斷是否成功
if(response.getStatusLine().getStatusCode()==200){
//通過EntityUtils將返回內容轉化成字符串
String content=EntityUtils.toString(response.getEntity(), "ytf-8");
//發送給mHandler
Message msg = mHandler.obtainMessage(SHOW_RESPONSE,
content); mHandler.sendMessage(msg);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
結果略