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();
}
}
}
}
私はこれに本当に慣れていないので、エラーがばかげている場合は申し訳ありません。
事前に助けてくれてありがとう。