我想从doInBackground方法返回数据并与其他方法中的其他字符串进行比较

时间:2017-03-12 12:57:56

标签: java android android-layout android-studio

我在此代码中做的是将服务器中检索到的数据与其他字符串进行比较,请告诉我更改。

ArrayList<HashMap<String, String>> matchStudentsList = new ArrayList<HashMap<String, String>>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.third_sem);
    present = (Button) findViewById(R.id.button2);

    bt= BluetoothAdapter.getDefaultAdapter();

    // Call Async task to get the match fixture
    new GetFixture().execute();

}
private class GetFixture extends AsyncTask<String, Void, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected String doInBackground(String... arg0) {
        ServiceHandler serviceClient = new ServiceHandler();
        Log.d("url: ", "> " + URL_Students);
        String json = serviceClient.makeServiceCall(URL_Students,ServiceHandler.GET);
        // print the json response in the log
        Log.d("Get match fixture response: ", "> " + json);
        if (json != null) {
            try {
                Log.d("try", "in the try");
                JSONObject jsonObj = new JSONObject(json);
                Log.d("jsonObject", "new json Object");
                // Getting JSON Array node
                matchRecords = jsonObj.getJSONArray(TAG_Table);
                Log.d("json aray", "user point array");
                int len = matchRecords.length();
                Log.d("len", "get array length");
                for (int i = 0; i <len; i++) {
                    JSONObject c = matchRecords.getJSONObject(i);
                    String RollNo = c.getString(TAG_Roll_No);
                    Log.d("RollNo", RollNo);
                    String FirstName = c.getString(TAG_First_Name);
                    Log.d("FirstName", FirstName);
                    String LastName = c.getString(TAG_Last_Name);
                    Log.d("LastName", LastName);
                    //  hashmap for single match
                    HashMap<String, String> matchFixture = new HashMap<String, String>();
                    // adding each child node to HashMap key => value
                    matchFixture.put(TAG_Roll_No, RollNo);
                    matchFixture.put(TAG_First_Name, FirstName);
                    matchFixture.put(TAG_Last_Name, LastName);
                    matchStudentsList.add(matchFixture);


                }



            }
            catch (JSONException e) {
                Log.d("catch", "in the catch");
                e.printStackTrace();
            }
        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }
        return null;
    }




    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);




         ListAdapter adapter = new SimpleAdapter(
                    ThirdSem.this, matchStudentsList,
                    R.layout.list_checkitem, new String[]{TAG_Roll_No, TAG_First_Name, TAG_Last_Name, TAG_BAddress}
                    , new int[]{R.id.RollNo, R.id.FirstName, R.id.LastName }

        );
            setListAdapter(adapter);

    }


}



}

对我来说非常有帮助

2 个答案:

答案 0 :(得分:0)

您在所有if语句中返回一个空值,一旦完成for循环,就应该返回matchStudentsList,否则返回null

在你的for循环之后

,添加:

return matchStudentsList;

并将您的onPostExecute签名更改为:

protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

还将AsyncTask定义更改为:

AsyncTask<String, Void, ArrayList<HashMap<String, String>>>

答案 1 :(得分:0)

您可以通过类似的方式获取doInBackground()字符串。

String desiredString = new GetFixture().execute().get();

在onCreate()方法中调用上面的内容。而不是:

new GetFixture().execute();