私は JAVA は初めてですが、C/C++ などの他の言語の経験があります。私はアンドロイドを勉強していて、JSON Web サービスにアクセスするアプリケーションを作成しています。
私のクラスコードは以下の通りです:
public class AsycTaskCall {
private final JSONObject jsonObject;
private final Context context;
public static String results;
public AsycTaskCall(Context ctx, JSONObject jobject)
{
this.jsonObject = jobject;
this.context = ctx;
}
public String call()
{
new WebServiceTask().execute("");
Log.d("RESULTS_CALL", "Results : " + results); //results is null here
return this.results;
}
private class WebServiceTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... args) {
JsonWebService jsonWebService = new JsonWebService(context);
return jsonWebService.callWebService(jsonObject);
}
protected void onPostExecute(String result) {
try
{
JSONObject jo = new JSONObject(result);
String status = jo.getString("success");
results = result;
if (status == "true")
{
Log.d("RESULTS", " Result : " + result + " : Results " + results); // results is fine here also
}
else
{
Log.d("RESULT_STATUS", "False");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
このクラスを作成したのは、私のアプリケーションが多くの場所で Web サービスを呼び出すためです。そのため、単一のクラスを作成して JSONObject を渡し、このクラスが Web サービスの非同期を呼び出します。
すべてが正常に機能しており、必要な結果が得られましたが、結果を呼び出し元のアクティビティに返したいと考えています。この目的のために、私は
public static String results;
プロパティを変更し、webservice から返された文字列をこの変数に保存します。この変数にはサブクラス WebServiceTask にデータがありますが、この同じ変数のデータはメイン クラスで null であるか、メイン クラス call function で返すと null です。
この問題を解決する方法を教えてください。
ありがとうございました