自动刷新textview

时间:2014-01-12 10:37:36

标签: android

我想创建一个应用。从json中的Server获得分数并将其显示在我的应用程序上。但自动刷新不起作用。 当它自动刷新时,它显示两个旧值和更新值。

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

    }
}

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



    }






}

}

1 个答案:

答案 0 :(得分:0)

检查我的评论你在conning循环中运行

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