如何从异步任务中的JSON中提取数据?

时间:2016-10-21 02:01:32

标签: java android json asynchronous android-asynctask

我的应用程序将数据从API提取到列表视图。这很好。单击应该显示更多数据的行时,它可以正常工作。但是,没有实际数据只显示空值。

我相信我在Async任务中的doInBackground方法中没有做任何事情。我相信它与URL和ID有关吗?

由于我是Java的新手,Haven能够整天用自我研究来解决这个问题。感谢。

MainActivity.java中的代码:

public void showMemberDetailsScreen(int _id) {
    mMembersListScreen.setVisibility(View.GONE);
    mMemberDetailsScreen.setVisibility(View.VISIBLE);

    if (NetworkUtils.isConnected(this)) {
        GetDetailsTask task = new GetDetailsTask(this);
        task.execute(_id);
    } else {
        Log.i(TAG, "onCreate: NOT CONNECTED");
    }
}

/**
 * Populate the member details screen with data.
 * 
 * @param _name
 * @param _birthday
 * @param _gender
 * @param _twitterId
 * @param _numCommittees
 * @param _numRoles
 */
public void populateMemberDetailsScreen(String _name, String _birthday, String _gender, 
        String _twitterId, String _numCommittees, String _numRoles) {

    TextView tv = (TextView)mMemberDetailsScreen.findViewById(R.id.text_name);
    Log.i(TAG, "populateMemberDetailsScreen: NAME = " + _name);
    tv.setText(_name);

    tv = (TextView)mMemberDetailsScreen.findViewById(R.id.text_birthday);
    tv.setText(_birthday);

    tv = (TextView)mMemberDetailsScreen.findViewById(R.id.text_gender);
    tv.setText(_gender);

    tv = (TextView)mMemberDetailsScreen.findViewById(R.id.text_twitter_id);
    tv.setText(_twitterId);

    tv = (TextView)mMemberDetailsScreen.findViewById(R.id.text_num_committees);
    tv.setText(_numCommittees);

    tv = (TextView)mMemberDetailsScreen.findViewById(R.id.text_num_roles);
    tv.setText(_numRoles);
}

OnItemClickListener mItemClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> _parent, View _view, int _position, long _id) {
        // TODO: Show the members detail screen
        Log.i(TAG, "onItemClick: RAN");

        showMemberDetailsScreen((int)_id);
        Log.i(TAG, "onItemClick: POSITION = " + _position);

    }
};

GetDetailsTask.java中的代码:

public class GetDetailsTask extends AsyncTask<Integer, Void, HashMap<String, String>> {
private static final String TAG = "GetDetailsTask";
private static final String API_URL = "https://www.govtrack.us/api/v2/person/ ";

private static final String NAME = "name";
private static final String BIRTHDAY = "birthday";
private static final String GENDER = "gender";
private static final String TWITTER_ID = "twitter_id";
private static final String NUM_COMMITTEES = "num_committees";
private static final String NUM_ROLES = "num_roles";

private MainActivity mActivity;

public GetDetailsTask(MainActivity _activity) {
    mActivity = _activity;
}

@Override
protected HashMap<String, String> doInBackground(Integer... _params) {

    // Add member ID to the end of the URL
    String data = NetworkUtils.getNetworkData(API_URL + _params[0]);

    HashMap<String, String> retValues = new HashMap<String, String>();

    try {
        JSONObject response = new JSONObject(data);

        String name = response.optString("name");
        Log.i(TAG, "doInBackground: NAME = " + name);
        retValues.put(NAME, name);

        String birthday = response.optString("birthday");
        retValues.put(BIRTHDAY, birthday);

        String gender = response.optString("gender_label");
        retValues.put(GENDER, gender);

        String twitterId = response.optString("twitterid");
        retValues.put(TWITTER_ID, twitterId);

        if (response.has("committeeassignments")) {
            JSONArray committeeArray = response.optJSONArray("committeeassignments");
            int numCommittees = committeeArray.length();
            retValues.put(NUM_COMMITTEES, "" + numCommittees);
            Log.i(TAG, "doInBackground: NUM COMMITTESS = " + numCommittees);
        } else {
            retValues.put(NUM_COMMITTEES, "" + 0);
        }

        if (response.has("roles")){
            JSONArray rolesArray = response.optJSONArray("roles");
            int numRoles = rolesArray.length();
            retValues.put(NUM_ROLES, "" + numRoles);
        } else {
            retValues.put(NUM_ROLES, "" + 0);
        }

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

@Override
protected void onPostExecute(HashMap<String, String> _result) {
    super.onPostExecute(_result);
    if (_result != null) {
        String name = _result.get(NAME);
        String birthday = _result.get(BIRTHDAY);
        String gender = _result.get(GENDER);
        String twitterId = _result.get(TWITTER_ID);
        String numCommittees = _result.get(NUM_COMMITTEES);
        String numRoles = _result.get(NUM_ROLES);

        mActivity.populateMemberDetailsScreen(name, birthday, gender, twitterId, numCommittees, numRoles);
    }
}

}

Member.java中的代码:

public class Member {

private int mId;
private String mName;
private String mParty;

public Member() {
    mId = 0;
    mName = mParty = "";
}

public Member(int _id) {
    this();
    mId = _id;
}

public Member(int _id, String _name) {
    this(_id);
    mName = _name;
}

public Member(int _id, String _name, String _party) {
    this(_id, _name);
    mParty = _party;
}

public void setId(int _id) {
    mId = _id;
}

public int getId() {
    return mId;
}

public void setName(String _name) {
    mName = _name;
}

public String getName() {
    return mName;
}

public void setParty(String _party) {
    mParty = _party;
}

public String getParty() {
    return mParty;
}

}

1 个答案:

答案 0 :(得分:0)

创建一个MainView.java接口

using System;

class Program
{
    static void Main()
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(i);
        }
    }
}

然后在MainActivity中实现它。 在你的GetDetailsTask.java上:

public interface MainView {
void populateMemberDetailsScreen(String _name, String _birthday, String _gender, 
        String _twitterId, String _numCommittees, String _numRoles);
void showMemberDetailsScreen(int _id);
}

onPostExecute:

MainView _view;
public GetDetailsTask(MainView _view) {
    this._view = _view;
}