将用户登录邮箱地址传递给下一个 FragmentActivity

Pass the user login email address to next FragmentActivity

当我的 LoginAsyncTask 调用 ActivityMenu.java 时,我想用它发送电子邮件地址,如代码所示。但电子邮件的值为空。我尝试了电子邮件和电子邮件,但它的值仍然为空。

我要发送这个的目的是我想在下一个屏幕中显示登录的用户名。

   public class LoginAsyncTask extends AsyncTask<String, Integer, JSONObject> {

    private JSONObject responseJson = null;
    private Context contxt;
    private Activity activity;
    String email;

    public LoginAsyncTask(Context context) {

        // API = apiURL;
        this.contxt = context;
    }

    // async task to accept string array from context array
    @Override
    protected JSONObject doInBackground(String... params) {

        String path = null;
        String response = null;
        HashMap<String, String> request = null;
        JSONObject requestJson = null;
        DefaultHttpClient httpClient = null;
        HttpPost httpPost = null;
        StringEntity requestString = null;
        ResponseHandler<String> responseHandler = null;

        // get the email and password
        Log.i("Email", params[0]);
        Log.i("Password", params[1]);

        try {

            path = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            new URL(path);
        } catch (MalformedURLException e) {

            e.printStackTrace();
        }

        try {

            // set the API request
            request = new HashMap<String, String>();
            request.put(new String("Email"), params[0]);
            request.put(new String("Password"), params[1]);
            request.entrySet().iterator();

            // Store locations in JSON
            requestJson = new JSONObject(request);
            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost(path);
            requestString = new StringEntity(requestJson.toString()); // requestJson has the email address

            // sets the post request as the resulting string
            httpPost.setEntity(requestString);
            httpPost.setHeader("Content-type", "application/json");

            // Handles the response
            responseHandler = new BasicResponseHandler();
            response = httpClient.execute(httpPost, responseHandler);

            responseJson = new JSONObject(response);

        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        try {
            responseJson = new JSONObject(response);

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return responseJson;

    }

    @Override
    protected void onPostExecute(JSONObject result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        String myResJson;
        try {

            myResJson = responseJson.getString("Status");
            String test = myResJson;
            if (test.equals("200")) {
                Intent intent = new Intent(contxt, ActivityMenu.class);
                intent.putExtra("user", email); // email value is null
                contxt.startActivity(intent);
            } else {
                Toast.makeText(contxt,
                        "Login Error, invalid Email or Password", Toast.LENGTH_SHORT)
                        .show();
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

我们将不胜感激。

emailnull 因为没有分配在 doInBackground 接收到的值。所以添加

email= params[0];

doInBackground 中获取 email 中的值。

您没有分配电子邮件。 尝试:

@Override
protected JSONObject doInBackground(String... params) {
   ...
   ...
   email= params[0];
}