Android新手 - 无法从服务器响应中提取令牌

时间:2017-07-09 23:27:08

标签: java android

我运行此代码:

        protected Object doInBackground(Object[] objects) {
        HttpClient client = new HttpClient();
        client.get(get_token, new HttpResponseCallback() {
            @Override
            public void success(String responseBody) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        llHolder.setVisibility(View.VISIBLE);
                    }
                });
                token = responseBody;

            }

而responseBody给了我回复:

{"response":{"token":"eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmlJ9","status":"success"}}

如何将“token”作为字符串,“status”

相同

1 个答案:

答案 0 :(得分:2)

使用此代码。

   protected Object doInBackground(Object[] objects) {
        HttpClient client = new HttpClient();
        client.get(get_token, new HttpResponseCallback() {
            @Override 
            public void success(String responseBody) {

                runOnUiThread(new Runnable() {
                    @Override 
                    public void run() { 

                        llHolder.setVisibility(View.VISIBLE);

                    try {
                        JSONObject jObject = new JSONObject(responseBody);
                        String token = jObject.getString("token");

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

                    } 
                }); 
            } 

了解详情this answer