本文介绍了自动刷新的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个应用程序。其中从服务器获取得分JSON和我的应用程序显示。但自动刷新不工作。
当它得到自动刷新它显示两个值旧的和更新后的值了。
I want to create one app. which get score from Server in json and display it on my app. but auto refresh is not working.when it get auto refresh it's showing two values old one and updated value too.
String s1=" ";
String s2=" ";
String s3=" ";
String s4=" ";
String s5=" ",s6=" ",s7=" ",s8=" ",s9=" ",s10=" ",s11=" ",s12=" ",s13=" ";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultview1=(TextView) findViewById(R.id.team1_1);
resultview2=(TextView) findViewById(R.id.team1_2);
resultview3=(TextView) findViewById(R.id.date);
resultview4=(TextView) findViewById(R.id.comm1);
resultview5=(TextView) findViewById(R.id.team2_1);
resultview6=(TextView) findViewById(R.id.team2_2);
resultview7=(TextView) findViewById(R.id.comm2);
resultview8=(TextView) findViewById(R.id.team3_1);
resultview9=(TextView) findViewById(R.id.team3_2);
resultview10=(TextView) findViewById(R.id.comm3);
resultview11=(TextView) findViewById(R.id.team4_1);
resultview12=(TextView) findViewById(R.id.team4_2);
resultview13=(TextView) findViewById(R.id.comm4);
//new BackHelper().execute();
mHandler();
}
private void mHandler()
{
// TODO Auto-generated method stub
mHandler.postDelayed(updateTask, 15000);
}
public void getdata1()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s1= s1 +
"Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
"Over : "+json.getInt("t1_0");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata2()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s2= s2 +
"Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
"Over : "+json.getInt("t2_o");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata3()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s3= s3
+json.getString("date");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata4()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s4= s4
+json.getString("comm");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata5()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team2.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s5= s5 +
"Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
"Over : "+json.getInt("t1_0");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata6()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team2.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s6= s6 +
"Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
"Over : "+json.getInt("t2_o");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata7()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team2.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s7= s7+json.getString("comm");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata8()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team3.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s8= s8 +
"Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
"Over : "+json.getInt("t1_0");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata9()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team3.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s9= s9 +
"Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
"Over : "+json.getInt("t2_o");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata10()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team3.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s10= s10 +json.getString("comm");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata11()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team4.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s11= s11 +
"Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
"Over : "+json.getInt("t1_0");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata12()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team4.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s12= s12 +
"Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
"Over : "+json.getInt("t2_o");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata13()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team4.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s13= s13 +json.getString("comm");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
私人处理器mHandler =新的处理程序();
private Handler mHandler = new Handler();
private Runnable updateTask = new Runnable ()
{
public void run()
{
Log.d(getString(R.string.app_name) + " ChatList.updateTask()",
"updateTask run!");
// run any code here...
//getdata();
new BackHelper().execute();
Log.e("hell", "hello");
// queue the task to run again in 15 seconds...
mHandler.postDelayed(updateTask, 15000);
}
};
class BackHelper extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params)
{
getdata1();
getdata2();
getdata3();
getdata4();
getdata5();
getdata6();
getdata7();
getdata8();
getdata9();
getdata10();
getdata11();
getdata12();
getdata13();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//super.onPostExecute(result1);
resultview1.setText(s1);
resultview2.setText(s2);
resultview3.setText(s3);
resultview4.setText(s4);
resultview5.setText(s5);
resultview6.setText(s6);
resultview7.setText(s7);
resultview8.setText(s8);
resultview9.setText(s9);
resultview10.setText(s10);
resultview11.setText(s11);
resultview12.setText(s12);
resultview13.setText(s13);
}
}
}
推荐答案
检查我的评论乌尔默记循环中运行
check my comment ur running in conning loop
private Runnable updateTask = new Runnable ()
{
public void run()
{
Log.d(getString(R.string.app_name) + " ChatList.updateTask()",
"updateTask run!");
// run any code here...
//getdata();
new BackHelper().execute();
Log.e("hell", "hello");
// queue the task to run again in 15 seconds...
// here i think your in continues loop. check
mHandler.postDelayed(updateTask, 15000);
}
};
这篇关于自动刷新的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!