AsyncTask执行一次(仅在启动应用程序时)

时间:2014-09-18 05:23:45

标签: java android android-asynctask

我真的不知道这是否是AsyncTask问题,但对于我所看到的情况,似乎这个类似乎缺少了一些东西。

我有以下代码:

package com.example.webserviceejemplo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class ActividadPrincipal extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actividad_principal);

        // Objectos de la vista
        final TextView txtvFeed = (TextView) findViewById(R.id.txtvFeed);
        final EditText txtXMLUrl = (EditText) findViewById(R.id.txtXMLUrl);
        final Button btnMostrarXML = (Button) findViewById(R.id.btnMostrarXML);

        // Scroll vertical (mas una propiedad en el XML)
        txtvFeed.setMovementMethod(new ScrollingMovementMethod());

        btnMostrarXML.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) 
            {
                new XMLHandler().execute(txtXMLUrl.getText().toString());
            }
        });

    }

    private class XMLHandler extends AsyncTask<String, Void, String>
    {

        @Override
        protected String doInBackground(String... urls) 
        {
            String xml = null;
            try 
            {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(urls[0]);
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                xml = EntityUtils.toString(httpEntity);
            } 
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            } 
            catch (ClientProtocolException e) 
            {
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            return xml;
        }

        @Override
        protected void onPostExecute(String result) 
        {  
            setContentView(R.layout.actividad_principal);
            TextView txtvFeed = (TextView) findViewById(R.id.txtvFeed);
            txtvFeed.setText(result);
        }
    }
}

这个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${packageName}.${activityClass}" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/url_to_show"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/txtXMLUrl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textUri" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnMostrarXML"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:text="@string/show_xml" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txtvFeed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical"
                android:text="@string/feed_to_display" />
        </LinearLayout>

    </ScrollView>

</LinearLayout>

我想要做的是单击按钮并在TextView中显示远程XML的内容(是的,不解析它)&#34; txtvFeed&#34;。现在,问题一切正常,但只是你第一次这样做,我的意思是当你启动应用程序时它只能工作一次。然后,如果您在EditText中输入另一个Feed网址并点击该按钮,则无法显示该Feed的内容。事实上,我不知道在显示内容后正在做什么,因为我根本没有收到任何消息。

我完全不知道问题是什么。我只有1个布局和1个.java,只是为了让你知道。

欢迎任何帮助,因为我是Android新手:)。

2 个答案:

答案 0 :(得分:1)

试试这段代码。它应该工作..

public class ActividadPrincipal extends Activity 
{
    final TextView txtvFeed;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actividad_principal);

    // Objectos de la vista
    txtvFeed = (TextView) findViewById(R.id.txtvFeed);
    final EditText txtXMLUrl = (EditText) findViewById(R.id.txtXMLUrl);
    final Button btnMostrarXML = (Button) findViewById(R.id.btnMostrarXML);

    // Scroll vertical (mas una propiedad en el XML)
    txtvFeed.setMovementMethod(new ScrollingMovementMethod());

    btnMostrarXML.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            new XMLHandler().execute(txtXMLUrl.getText().toString());
        }
    });

}

private class XMLHandler extends AsyncTask<String, Void, String>
{

    @Override
    protected String doInBackground(String... urls) 
    {
        String xml = null;
        try 
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(urls[0]);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);
        } 
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        } 
        catch (ClientProtocolException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        return xml;
    }

    @Override
    protected void onPostExecute(String result) 
    {  
        txtvFeed.setText(result);
    }
}

}

答案 1 :(得分:1)

删除asynctask类中postexecute中的setcontentview,并仅在一次声明和定义textview,两次使用。在全球范围内初始化textview,

TextView txtvFeed;  //globally

txtvFeed = (TextView) findViewById(R.id.txtvFeed);

像这样改变postexecute,

 @Override
protected void onPostExecute(String result) 
{  
    txtvFeed.setText(result);
}