将用户登录电子邮件地址传递给下一个片段活动



当我的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();
        }
    }
对此的

帮助将不胜感激。

email null,因为没有分配在doInBackground接收的值。 所以添加

email= params[0];

doInBackground获得价值email.

您不会为电子邮件分配任务。尝试:

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

最新更新