2015-02-11 101 views
0

當我LoginAsyncTask調用ActivityMenu.java我想要發送的電子郵件地址,如代碼中所示。但電子郵件的值爲空。我嘗試了電子郵件和電子郵件,但它的值仍爲空。將用戶登錄電子郵件地址傳遞給下一個FragmentActivity

目的我想發送這是我想顯示在下一個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(); 
     } 

    } 

有助於實現此目的將高度讚賞。

回答

1

emailnull,因爲沒有分配在doInBackground中接收的值。所以在doInBackground添加

email= params[0]; 

email中獲取價值。

+0

日Thnx它的工作.. – 2015-02-11 07:50:18

+0

@JohnDavid:歡迎約翰:) – 2015-02-11 07:52:19

0

您不會分配電子郵件。 嘗試:

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