将用户登录电子邮件地址传递给下一个FragmentActivity

时间:2015-02-11 07:37:32

标签: android login logout

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

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

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

    }

帮助实现这一目标将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

emailnull,因为未分配在doInBackground中接收的值。所以添加

email= params[0];
doInBackground

获取email中的值。

答案 1 :(得分:0)

您不会对电子邮件进行分配。 尝试:

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