从类返回JSON对象数组

时间:2014-09-18 00:47:39

标签: java android android-studio mobile-application

我最近开始尝试自学如何编写Android应用程序,我在理解/实现从我正在调用的类传递结果数组的方法时遇到一些困难。

由于我不确定如何完美地布置这个例子,请耐心等待我!

我的MainActivity正在调用类AsyncTaskParseJson(这是部分Frankensteined来自在线示例)。现在,我的MainActivity可以调用该类,并返回一个显示true / false的字符串。我希望将一个数组或一个转换为字符串的数组返回给我的MainActivity进行进一步处理。

代码:

package jesse.jones.test;

import android.os.AsyncTask;
import android.util.Log;

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

import java.util.ArrayList;

/**
 * Created by Shadow on 9/16/14.
 */

// you can make this class as another java file so it will be separated from your main activity.
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

    final String TAG = "AsyncTaskParseJson.java";
    String userEmail;
    String userPassword;
    String doAction;
    String reportArray;

    // set your json string url here
    String yourJsonStringUrl = <-url removed->;

    // contacts JSONArray
    JSONArray dataJsonArr = null;

    MainActivity mActivity;

    public AsyncTaskParseJson(MainActivity activity){
        mActivity = activity;


    }

    public void setCredentials(String inUserEmail,String inUserPassword){
        userEmail = inUserEmail;
        userPassword= inUserPassword;
    }

    public void setAction(String actionToTake){
        doAction = actionToTake;
    }

    protected String urlBuilderv1(String actionToTake,String inUserEmail, String inUserPassword){
        String urlString = <-url removed->

        if (actionToTake == "test"){
            urlString = <-url removed->
        }
        if (actionToTake == "login"){
            urlString = <-url removed->
        }

        if (actionToTake == "getReports"){
            urlString = <-url removed->
        }

        return urlString;
    }

    public String getReportArray(){

        return reportArray;
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected String doInBackground(String... arg0) {

        String jsonReturnedResult = "NORETURN";
        String jsonReturnedMessage = null;
        String jsonReturnedReports = null;
        String result = null;

        try {

            // instantiate our json parser
            JasonParser jParser = new JasonParser();

            yourJsonStringUrl = urlBuilderv1(doAction,userEmail,userPassword);

            // get json string from url
            JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);


            jsonReturnedResult = json.get("result").toString();
            //^^ I would like to take this JSONArray back to main activity
            //^^ instead of pulling a single string from it.
            result = jsonReturnedResult;


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

        return result;
    }
    @Override
    protected void onPostExecute(String strFromDoInBg) {

        mActivity.onGetResults(strFromDoInBg);


    }

}

1 个答案:

答案 0 :(得分:0)

调用execute()方法后,在任务引用上调用get()方法。这基本上会把它变成一个同步任务,但它会得到String结果。 AsyncTask返回的类型是您在“extends AsyncTask”中指定的最后一个“String”。将其更改为您想要返回的任何类型。