我提取json字符串。下一个活动的Json响应的e认证令牌。

时间:2022-04-15 20:50:09

*on postExecute()

*在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 Response which is not fetching me the value i want to stored as a variable userToken

*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 个解决方案

#1


1  

Try this:

试试这个:

   @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();
            }
        }
    }
}

#2


0  

In you postexecute

在你postexecute

You must parse your json dont use substring

你必须解析你的json不要使用子字符串

    @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();
        }
    }
}
}

Note: Use SharedPrefernce to store your user data so that is can be accessed from anywhere

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

#1


1  

Try this:

试试这个:

   @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();
            }
        }
    }
}

#2


0  

In you postexecute

在你postexecute

You must parse your json dont use substring

你必须解析你的json不要使用子字符串

    @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();
        }
    }
}
}

Note: Use SharedPrefernce to store your user data so that is can be accessed from anywhere

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