簡體   English   中英

登錄狀態成功后打算進行HomeActivity

[英]Intent to HomeActivity after login status success

這是我的LoginActivity.java

這會將登錄詳細信息發送到HttpAsyncTask .java

final Button button = (Button) findViewById(R.id.btnLogin);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            try {

                // Get Email Edit View Value
                String email = emailET.getText().toString();
                // Get Password Edit View Value
                String password = pwdET.getText().toString();
                // Instantiate Http Request Param Object
                //                  RequestParams params = new RequestParams();
                // When Email Edit View and Password Edit View have values
                // other than Null
                if (Utility.isNotNull(email) && Utility.isNotNull(password)) {
                    // When Email entered is Valid
                    if (Utility.validate(email)) {

                        //call the async task

                        JSONObject js = new HttpAsyncTask(getApplicationContext()).execute(email,password).get();

                        Toast.makeText(getApplicationContext(), "Asynctask started", Toast.LENGTH_SHORT).show();

                    }
                    // When Email is invalid
                    else {
                        Toast.makeText(getApplicationContext(),
                                "Please enter valid email",
                                Toast.LENGTH_LONG).show();
                    }
                }
                // When any of the Edit View control left blank
                else {
                    Toast.makeText(
                            getApplicationContext(),
                            "Please fill the form, don't leave any field blank",
                            Toast.LENGTH_LONG).show();
                }
            } catch (Exception ex) {

            }

        }
    });
  }
}

HttpAsyncTask.java

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

private static InputStream stream = null;
private static String API;
private JSONObject responseJson = null;
private Context contxt;
private Activity activity;

public HttpAsyncTask(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 username and password
    Log.i("Email", params[0]);
    Log.i("Password", params[1]);

    try {

        path = "http://192.168.XXXXXXX";
        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();


        requestJson = new JSONObject(request);
        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(path);
        requestString = new StringEntity(requestJson.toString());


        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);
    Log.d("MyAsyncTask", "Received result: " + result); //here i get Received result: {"status":"400"}

}

我的問題是我想知道登錄成功后如何將其定向到ActivityHome ,如果登錄錯誤,則應顯示錯誤消息。

我在onPostExecute方法中嘗試過

activity.startActivity(new Intent(activity, ActivityHome.class));

但它不起作用,應用程序崩潰了。

首先,您必須將響應存儲在onPostExecute方法中的JSONObject中。

protected void onPostExecute(JSONObject result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    if(responseJson!=null){
        try{
            JSONObject jObjstatus=new JSONObject(responseJson);
            String status=jObjstatus.getString("status");
            if(status.equals("success"){
                startActivity(new Intent(MainActivity.this, SecondActivity.class);
            }
        }catch(){
        }
    }
}

公共類HttpAsyncTask擴展了AsyncTask {

private static InputStream stream = null;
private static String API;
private JSONObject responseJson = null;
private Context contxt;
private Activity activity;

public HttpAsyncTask(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 username and password
    Log.i("Email", params[0]);
    Log.i("Password", params[1]);

    try {

        path = "http://192.168.XXXXXXX";
        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();


        requestJson = new JSONObject(request);
        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(path);
        requestString = new StringEntity(requestJson.toString());


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


    return responseJson;
}

@Override
protected void onPostExecute(JSONObject result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    if (result != null) {
        String status = result.optString("status");
        if (status.equalsIgnoreCas("200"))
        {
            ((Activity) context).startActivity(new Intent(context, HomeActivity.class));

        }else
        {
            //error message
        }
    } else {
        //error message
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM