从Json响应中提取json字符串,即身份验证令牌以进行下一个活动。

时间:2016-07-29 05:05:17

标签: android json android-studio

* on postExecute()

@Override
        protected void onPostExecute(String s) {
            if (s != null) {
                s.replaceAll("\\s+","");
                Log.e("Fetch_frame_response", s);
                userToken = s.substring(24,451);
                Log.e("userToken", userToken);
                Intent abc = new Intent(MainActivity.this, Orderlist.class);
                startActivity(abc);
                finish();
            } else {
                Intent abc = new Intent(MainActivity.this, MainActivity.class);
                startActivity(abc);
                Toast.makeText(MainActivity.this, "Invalid credentials", Toast.LENGTH_LONG).show();
            }
        }
    }
}

* logcat响应,它没有提取我想要存储为变量userToken的值

07-29 10:30:57.556 7341-7341/com.example.rinzinchoephel.driverdemo2 E/userToken: 2791d970-c11d-48f6-92f3-8ec4f0b14820","contactEmail":"rchomphel@gmail.com","contactPhones":"+19731488021","createdAt":"2016-07-04T01:42:11.452-05:00","description":"","email":"rchomphel@gmail.com","id":"577a051469702d0380170000","internalId":"rinzin","lastAccessedOn":"2016-07-04T01:42:11.452-05:00","name":"rinzin choephel","phone":"+19731488021","updatedAt":"2016-07-04T01:42:11.452-05:00","userType":"merchant","username":"r

2 个答案:

答案 0 :(得分:1)

试试这个:

   @Override
        protected void onPostExecute(String s) {
            if (s != null) {
                Log.e("Fetch_frame_response", s);
                try {
                    JSONObject object=new JSONObject(s);
                    String userToken=object.getString("authenticationToken");
                    Log.e("userToken",userToken);
                } catch (JSONException e) {
                    e.printStackTrace();
                }  
                Intent abc = new Intent(MainActivity.this, Orderlist.class);
                startActivity(abc);
                finish();
            } else {
                Intent abc = new Intent(MainActivity.this, MainActivity.class);
                startActivity(abc);
                Toast.makeText(MainActivity.this, "Invalid credentials", Toast.LENGTH_LONG).show();
            }
        }
    }
}

答案 1 :(得分:0)

在你发布后

你必须解析你的json不要使用substring

    @Override
    protected void onPostExecute(String s) {
        if (s != null) {

             try {
                        JSONObject responseObject = new JSONObject(s);
                        int status = responseObject.getInt("status");
                        String message =responseObject.getString("message");
                        if(status == 1){
                        String UserToken =   responseObject.getString("userToken");         
 Log.i("userToken",UserToken );    
 Intent abc = new Intent(MainActivity.this, Orderlist.class);
 abc.putExtra("userToken",UserToken);
            startActivity(abc);
            finish();                 
                                 Toast.makeText(context,message,Toast.LENGTH_LONG).show();
                        }else if(status ==2){
                               Toast.makeText(context,message,Toast.LENGTH_LONG).show();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


        } else {
            Intent abc = new Intent(MainActivity.this, MainActivity.class);
            startActivity(abc);
            Toast.makeText(MainActivity.this, "Invalid credentials", Toast.LENGTH_LONG).show();
        }
    }
}
}

注意:使用SharedPrefernce存储您的用户数据,以便可以从任何地方访问