前言
歡迎大家我分享和推薦好用的代碼段~~
聲明
歡迎轉載,但請保留文章原始出處:
CSDN:http://www.csdn.net
雨季o莫憂離:http://blog.csdn.net/luckkof
正文
首先,創建一個顯示系統當前時間的jsp網頁文件,代碼如下:
- <html>
- <head>
- <title>My JSP 'index.jsp' starting page</title>
- </head>
- <body>
- <%
- String type = request.getParameter("par");
- String result = new String(type.getBytes("iso-8859-1"),"gb2312");
- out.println("<h1>parameters:"+result+"</h1>");
- %>
- </body>
- </html>
- package com.android;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private final String TAG = "MainActivity" ;
- private TextView textView;
- private Button button;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.http);
- textView = (TextView)findViewById(R.id.text);
- button = (Button)findViewById(R.id.button);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- refresh();
- }
- });
- new Thread(mRunnable).start();
- }
- //刷新網頁顯示
- private void refresh(){
- String httpUrl = "http://127.0.0.1:8080/test/currentDate.jsp";
- String result = "";
- URL url = null;
- try{
- //構造一個URL對象
- url = new URL(httpUrl);
- }catch (MalformedURLException e) {
- Log.e(TAG, "MalformedURLException");
- e.printStackTrace();
- }
- if(url != null){
- try{
- //使用httpURLConnection打開連接
- HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
- //得到讀取的內容
- InputStreamReader inReader = new InputStreamReader(urlConnection.getInputStream());
- //爲輸出創建BufferedReader
- BufferedReader bufferedReader = new BufferedReader(inReader);
- String line = null;
- //使用循環來讀取獲得的數據
- while((line=bufferedReader.readLine())!=null){
- result += line +"\n";
- }
- //關閉InputStreamReader
- inReader.close();
- //關閉Http連接
- urlConnection.disconnect();
- if(result.equals("")){
- textView.setText("讀取的內容爲null");
- }else{
- textView.setText(result);
- }
- }catch (IOException e) {
- // TODO: handle exception
- Log.e(TAG, "IOException");
- e.printStackTrace();
- }
- }else{
- Log.e(TAG, "url null");
- }
- }
- private Runnable mRunnable = new Runnable() {
- @Override
- public void run() {
- while(true){
- try{
- Thread.sleep(5 * 1000);
- //發送消息
- handler.sendMessage(handler.obtainMessage());
- }catch (InterruptedException e) {
- // TODO: handle exception
- Log.e(TAG, "InterruptedException");
- e.printStackTrace();
- }
- }
- }
- };
- Handler handler = new Handler(){
- public void handleMessage(Message msg){
- super.handleMessage(msg);
- refresh();
- }
- };
- }