0

jsonがバックグラウンドで解析されている間に(非同期タスクを使用して)進行状況ダイアログを表示しようとしていますが、それを試みるたびに強制的に閉じると、非同期タスクなしで使用するたびにjsonが正常に機能します。

これが私のコードです:

package com.Parsing.SOAPParsing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final String SOAP_ACTION = "http://tempuri.org/testService";
    private static final String METHOD_NAME = "testService";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://testService/webservice.asmx";

    TextView tv, optionA, optionB, optionC, optionD, optionE;
    ArrayList<HashMap<String, String>> testList = new ArrayList<HashMap<String, String>>();

    private String result;

    int j = 0;
    int k;
    String questions[] = new String[12];

    String opA[] = new String[12];
    String opB[] = new String[12];
    String opC[] = new String[12];
    String opD[] = new String[12];
    String opE[] = new String[12];

    ListView list;
    Button next;
    Button previous;
    int i;
    String v;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView) findViewById(R.id.questions);

        Fetch fetch = new Fetch(); // Calling async task
        fetch.execute();

    }

    public class Fetch extends AsyncTask<String, Void, Void> {

        private ProgressDialog dialog = new ProgressDialog(MainActivity.this);

        @Override
        public void onPreExecute() {
            this.dialog
                    .setMessage("Loading database. Please wait..."
                            + "\n\n\n\n This will will only load for once when you install the application");
            this.dialog.show();
        }

        @Override
        public Void doInBackground(String... params) {
            for (k = 1; k <= 10; k++) {
                try {

                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    request.addProperty("QuestionId", Long.valueOf(k));

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);

                    HttpTransportSE androidHttpTransport = new HttpTransportSE(
                            URL);

                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapPrimitive response = (SoapPrimitive) envelope
                            .getResponse();
                    result = response.toString();

                    try {
                        JSONObject o = new JSONObject(result);
                        Iterator<String> it = o.keys();

                        while (it.hasNext()) {
                            JSONArray ja = o.optJSONArray(it.next());
                            if (ja != null) {

                                for (i = 0; i <= ja.length(); i++) {
                                    String v = ja.get(i).toString();
                                    Log.i("value", i + " = " + v);

                                    if (i == 0) {
                                        opA[k - 1] = v;
                                    }

                                    if (i == 1) {
                                        opB[k - 1] = v;
                                    }
                                    if (i == 2) {
                                        opC[k - 1] = v;
                                    }
                                    if (i == 3) {
                                        opD[k - 1] = v;
                                    }
                                    if (i == 4) {
                                        opE[k - 1] = v;
                                    }

                                    if (i == 7) {

                                        questions[k - 1] = v;

                                    }
                                }
                            }

                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } catch (Exception e) {
                    tv.setText(e.getMessage());
                }
            }
            return null;
        }

        public void onPostExecute(final Void unsed) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
                Toast.makeText(MainActivity.this, opB[k], Toast.LENGTH_LONG)
                        .show();
            }
        }
    }
}

私はこれに本当に慣れていないので、エラーがばかげている場合は申し訳ありません。

事前に助けてくれてありがとう。

4

3 に答える 3

2

doInBackground から UI 要素にアクセスしているため

 @Override
   public Void doInBackground(String... params) {
  //YOUR CODE....
     } catch (Exception e) {

      tv.setText(e.getMessage()); //HERE YOU ARE ACCESSING TEXTVIEW FROM doInBackground

      }
//YOUR CODE...
于 2012-06-29T05:27:56.663 に答える
1

あなたができることは:

  • Asynctaskの戻りタイプをStringに変更します
  • doInBackgroundメソッドから必要な文字列を返します
  • この文字列をonPostExecuteメソッドのテキストビューに更新します

私はあなたのコードを以下のように更新しました。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.qualstream.telfaz.R;

public class MainActivity extends Activity {

    private static final String SOAP_ACTION = "http://tempuri.org/LoadQuestionDetail";
    private static final String METHOD_NAME = "LoadQuestionDetail";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://www.beyyondcareers.com/webservice.asmx";

    TextView tv, optionA, optionB, optionC, optionD, optionE;
    ArrayList<HashMap<String, String>> testList = new ArrayList<HashMap<String, String>>();

    private String result;

    int j = 0;
    int k;
    String questions[] = new String[12];

    String opA[] = new String[12];
    String opB[] = new String[12];
    String opC[] = new String[12];
    String opD[] = new String[12];
    String opE[] = new String[12];

    ListView list;
    Button next;
    Button previous;
    int i;
    String v;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        tv = (TextView) findViewById(R.id.textview123);

        Fetch fetch = new Fetch(); // Calling async task
        fetch.execute();

    }

    public class Fetch extends AsyncTask<Void, Void, String> {

        private ProgressDialog dialog = new ProgressDialog(MainActivity.this);

        @Override
        public void onPreExecute() {
            this.dialog
                    .setMessage("Loading database. Please wait..."
                            + "\n\n\n\n This will will only load for once when you install the application");
            this.dialog.show();
        }

        @Override
        public String doInBackground(Void... params) {
            for (k = 1; k <= 10; k++) {
                try {

                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    request.addProperty("QuestionId", Long.valueOf(k));

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);

                    HttpTransportSE androidHttpTransport = new HttpTransportSE(
                            URL);

                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapPrimitive response = (SoapPrimitive) envelope
                            .getResponse();
                    result = response.toString();

                    try {
                        JSONObject o = new JSONObject(result);
                        Iterator<String> it = o.keys();

                        while (it.hasNext()) {
                            JSONArray ja = o.optJSONArray(it.next());
                            if (ja != null) {

                                for (i = 0; i <= ja.length(); i++) {
                                    String v = ja.get(i).toString();
                                    Log.i("value", i + " = " + v);

                                    if (i == 0) {
                                        opA[k - 1] = v;
                                    }

                                    if (i == 1) {
                                        opB[k - 1] = v;
                                    }
                                    if (i == 2) {
                                        opC[k - 1] = v;
                                    }
                                    if (i == 3) {
                                        opD[k - 1] = v;
                                    }
                                    if (i == 4) {
                                        opE[k - 1] = v;
                                    }

                                    if (i == 7) {

                                        questions[k - 1] = v;

                                    }
                                }
                            }

                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } catch (Exception e) {
                    return e.getMessage();
                }
            }
            return "";
        }

        @Override
        public void onPostExecute(String result) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
                tv.setText(result);
                Toast.makeText(MainActivity.this, opB[k], Toast.LENGTH_LONG)
                        .show();
            }
        }
    }
}
于 2012-06-29T06:07:40.463 に答える
0

doInBackgroundメソッドからUI要素にアクセスすることはできません。また、 tv変数はUI要素です。

tv.setText(e.getMessage());

代わりに、onProgressUpdateメソッドを使用してUI要素にアクセスできます。doInBackgroundでpublishProgressを呼び出すと、onProgressUpdateメソッドが自動的に呼び出されます。

于 2012-06-29T05:58:08.337 に答える