Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

android - Auto Refresh textview

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 + "
");
        }
        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")+"
"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"
"+
                       "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 + "
");
        }
        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")+"
"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"
"+
                       "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 + "
");
        }
        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 + "
");
        }
        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 + "
");
        }
        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")+"
"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"
"+
                       "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 + "
");
        }
        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")+"
"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"
"+
                       "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 + "
");
        }
        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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}
 };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...