Android 定时到服务器取数据并刷新
来源:互联网 发布:mysql 安全审计 编辑:程序博客网 时间:2024/06/05 02:42
http://wen742538485.iteye.com/blog/1317548
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Test extends Activity implements Runnable{ /** Called when the activity is first created. */ private Button btn_get = null; private Button btn_post = null; private TextView tv_rp = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_get = (Button) this.findViewById(R.id.Button01); btn_post = (Button) this.findViewById(R.id.Button02); tv_rp = (TextView) this.findViewById(R.id.TextView); btn_get.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp?par=request-get"; HttpGet request = new HttpGet(httpUrl); HttpClient httpClient = new DefaultHttpClient(); try { HttpResponse response = httpClient.execute(request); if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ String str = EntityUtils.toString(response.getEntity()); tv_rp.setText(str); }else{ tv_rp.setText("请求错误"); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); btn_post.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp"; HttpPost request = new HttpPost(httpUrl); List<namevaluepair> params = new ArrayList<namevaluepair>(); params.add(new BasicNameValuePair("par","request-post")); try { HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8"); request.setEntity(entity); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(request); if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ String str = EntityUtils.toString(response.getEntity()); tv_rp.setText(str); }else{ tv_rp.setText("请求错误"); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); new Thread(this).start(); } public void refresh(){ String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp"; try { URL url = new URL(httpUrl); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.connect(); InputStream input = urlConn.getInputStream(); InputStreamReader inputreader = new InputStreamReader(input); BufferedReader reader = new BufferedReader(inputreader); String str = null; StringBuffer sb = new StringBuffer(); while((str = reader.readLine())!= null){ sb.append(str).append("\n"); } if(sb != null){ tv_rp.setText(sb.toString()); }else{ tv_rp.setText("NULL"); } reader.close(); inputreader.close(); input.close(); reader = null; inputreader = null; input = null; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Handler handler = new Handler(){ public void handleMessage(Message msg){ super.handleMessage(msg); refresh(); } }; public void run() { // TODO Auto-generated method stub while(true){ try { Thread.sleep(1000); handler.sendMessage(handler.obtainMessage()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
- Android 定时到服务器取数据并刷新
- Android以后台Service的方式获取GPS数据,并定时发送到服务器
- Android以后台Service方式获取GPS数据,并定时发送到服务器
- js+实现+无刷新定时取数据
- Android之定时刷新数据
- android 定时拉取数据
- Ajax定时从服务器取数据
- 用Python实现定时备份Mongodb数据,并上传到FTP服务器
- Android从服务器取数据
- 无进度条定时刷新获得服务器端数据并相应处理
- jquery定时刷新数据
- dataTable定时刷新数据
- linux定时备份mysql并同步到其它服务器
- linux定时备份mysql并同步到其它服务器
- linux定时备份mysql并同步到其它服务器
- 自动刷新并定时跳转
- android定时刷新界面
- android定时刷新界面
- ListView实现分页功能
- 博客之伊始 — xp下android环境搭建
- 基于XDS/XDS-I的区域医疗影像共享交换技术架构(2)
- 在Matlab中注释多行
- linux命令行下面访问网页
- Android 定时到服务器取数据并刷新
- 正则表达式学习日记
- latex插入图片
- ASP.NET 动态转静态页面的两种方法总结 - 动态页面转换成HTML静态页面方法
- android 编译和代码相关问题
- "SQL Server 安装程序无法获取 ASPNET 帐户的系统帐户信息"
- INNO Setup 使用笔记
- 107条Javascript的常用语句
- Android应用自动更新功能的代码实现