定时到服务器取数据并刷新
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.UnsupportedEncodingException;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.HttpStatus;
importorg.apache.http.NameValuePair;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.entity.UrlEncodedFormEntity;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.message.BasicNameValuePair;
importorg.apache.http.util.EntityUtils;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
publicclassTestextendsActivityimplementsRunnable{
/**Calledwhentheactivityisfirstcreated.*/
privateButtonbtn_get=null;
privateButtonbtn_post=null;
privateTextViewtv_rp=null;
@Override
publicvoidonCreate(BundlesavedInstanceState){
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(newButton.OnClickListener(){
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
StringhttpUrl="http://192.168.0.132:8080/Android/httpreq.jsp?par=request-get";
HttpGetrequest=newHttpGet(httpUrl);
HttpClienthttpClient=newDefaultHttpClient();
try{
HttpResponseresponse=httpClient.execute(request);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
Stringstr=EntityUtils.toString(response.getEntity());
tv_rp.setText(str);
}else{
tv_rp.setText("请求错误");
}
}catch(ClientProtocolExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
});
btn_post.setOnClickListener(newButton.OnClickListener(){
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
StringhttpUrl="http://192.168.0.132:8080/Android/httpreq.jsp";
HttpPostrequest=newHttpPost(httpUrl);
List<namevaluepair>params=newArrayList<namevaluepair>();
params.add(newBasicNameValuePair("par","request-post"));
try{
HttpEntityentity=newUrlEncodedFormEntity(params,"UTF-8");
request.setEntity(entity);
HttpClientclient=newDefaultHttpClient();
HttpResponseresponse=client.execute(request);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
Stringstr=EntityUtils.toString(response.getEntity());
tv_rp.setText(str);
}else{
tv_rp.setText("请求错误");
}
}catch(UnsupportedEncodingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ClientProtocolExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
});
newThread(this).start();
}
publicvoidrefresh(){
StringhttpUrl="http://192.168.0.132:8080/Android/httpreq.jsp";
try{
URLurl=newURL(httpUrl);
HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();
urlConn.connect();
InputStreaminput=urlConn.getInputStream();
InputStreamReaderinputreader=newInputStreamReader(input);
BufferedReaderreader=newBufferedReader(inputreader);
Stringstr=null;
StringBuffersb=newStringBuffer();
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(MalformedURLExceptione){
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
publicHandlerhandler=newHandler(){
publicvoidhandleMessage(Messagemsg){
super.handleMessage(msg);
refresh();
}
};
publicvoidrun(){
//TODOAuto-generatedmethodstub
while(true){
try{
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
</namevaluepair></namevaluepair>