【问题标题】:Using AsyncTask to display web service data in ListView使用 AsyncTask 在 ListView 中显示 Web 服务数据
【发布时间】:2015-07-23 06:56:27
【问题描述】:

喂,

当我单击按钮时,我从 Web 服务获取数据并将其发布到列表视图中,但我不想单击按钮。当我打开程序时,首先加载列表视图,然后它向我显示数据.

这是我的代码(此代码有效)

package com.example.karayel.wcf;


import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


public class MainActivity extends ActionBarActivity {

    private static final String SOAP_ACTION = "http://tempuri.org/IService/AllUserName";
    private static final String METHOD_NAME = "AllUserName";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2:50006/WCFService2/Service.svc?wsdl";
    String[] userNames = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button =(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread networkThread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                            envelope.bodyOut = request;
                            envelope.dotNet = true;
                            envelope.setOutputSoapObject(request);
                            HttpTransportSE ht = new HttpTransportSE(URL);
                            ht.call(SOAP_ACTION, envelope);
                            SoapObject result = (SoapObject)envelope.getResponse();
                            userNames = new String[result.getPropertyCount()];
                            for(int i=0;i<result.getPropertyCount();i++){
                                userNames[i]=result.getPropertyAsString(i);
                            }
                            runOnUiThread(new Runnable() {
                                public void run() {

                                    //(A) adım
                                    ListView listemiz=(ListView) findViewById(R.id.listView);

                                    //(B) adım
                                    ArrayAdapter<String> veriAdaptoru=new ArrayAdapter<String>
                                            (MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, userNames);

                                    //(C) adım
                                    listemiz.setAdapter(veriAdaptoru);
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
                networkThread.start();
            }
        });
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我想用 AsyncTask 重写代码。我该怎么做?

谢谢...

【问题讨论】:

    标签: android listview android-asynctask


    【解决方案1】:
    public class MainActivity extends AppCompatActivity {
    
        private static final String SOAP_ACTION = "http://tempuri.org/IService/AllUserName";
        private static final String METHOD_NAME = "AllUserName";
        private static final String NAMESPACE = "http://tempuri.org/";
        private static final String URL = "http://10.0.2.2:50006/WCFService2/Service.svc?wsdl";
        String[] userNames = null;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            new AsyncTask<Void , Void, String[]>(){
    
                @Override
                protected String[] doInBackground(Void... params) {
                    // TODO Auto-generated method stub
                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    envelope.bodyOut = request;
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE ht = new HttpTransportSE(URL);
                    ht.call(SOAP_ACTION, envelope);
                    SoapObject result = (SoapObject)envelope.getResponse();
                    userNames = new String[result.getPropertyCount()];
                    for(int i=0;i<result.getPropertyCount();i++){
                        userNames[i]=result.getPropertyAsString(i);
                    }
                    return userNames;
                }
                @Override
                protected void onPostExecute(String[] result){
                     //(A) adım
                    ListView listemiz=(ListView) findViewById(R.id.listView);
    
                    //(B) adım
                    ArrayAdapter<String> veriAdaptoru=new ArrayAdapter<String>
                            (MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, result);
    
                    //(C) adım
                    listemiz.setAdapter(veriAdaptoru);
                }
            }.execute();
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }
    

    【讨论】:

    • actionbar activity 已弃用。!
    • 糟糕!没有注意到只是复制他的代码。我的坏:)
    • @Melvin man 我几乎写了相同的答案,在发布之前看到了你的答案,干得好:)
    【解决方案2】:

    您有很多选择,其中一些如下所示:

    • 为此,您应该从onClick() 中删除所有代码,并将所有内容写入您活动的onCreate() 方法中。

    • 试试这个:findViewById(R.id.button).performClick();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 2018-02-12
      • 1970-01-01
      • 2018-05-15
      相关资源
      最近更新 更多