◎위챗 : speedseoul
two simple AsyncTasks that use the updated JSONParser class
http://danielnugent.blogspot.kr/2015/06/updated-jsonparser-with.html
class PostAsync extends AsyncTask<String, String, JSONObject> {JSONParser jsonParser = new JSONParser(); private ProgressDialog pDialog; private static final String LOGIN_URL = "http://www.example.com/testPost.php"; private static final String TAG_SUCCESS = "success"; private static final String TAG_MESSAGE = "message"; @Override protected void onPreExecute() { pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Attempting login..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected JSONObject doInBackground(String... args) { try { HashMap<String, String> params = new HashMap<>(); params.put("name", args[0]); params.put("password", args[1]); Log.d("request", "starting"); JSONObject json = jsonParser.makeHttpRequest( LOGIN_URL, "POST", params); if (json != null) { Log.d("JSON result", json.toString()); return json; } } catch (Exception e) { e.printStackTrace(); } return null; } protected void onPostExecute(JSONObject json) { int success = 0; String message = ""; if (pDialog != null && pDialog.isShowing()) { pDialog.dismiss(); } if (json != null) { Toast.makeText(MainActivity.this, json.toString(), Toast.LENGTH_LONG).show(); try { success = json.getInt(TAG_SUCCESS); message = json.getString(TAG_MESSAGE); } catch (JSONException e) { e.printStackTrace(); } } if (success == 1) { Log.d("Success!", message); }else{ Log.d("Failure", message); } } } class GetAsync extends AsyncTask<String, String, JSONObject> { JSONParser jsonParser = new JSONParser(); private ProgressDialog pDialog; private static final String LOGIN_URL = "http://www.example.com/testGet.php"; private static final String TAG_SUCCESS = "success"; private static final String TAG_MESSAGE = "message"; @Override protected void onPreExecute() { pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Attempting login..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected JSONObject doInBackground(String... args) { try { HashMap<String, String> params = new HashMap<>(); params.put("name", args[0]); params.put("password", args[1]); Log.d("request", "starting"); JSONObject json = jsonParser.makeHttpRequest( LOGIN_URL, "GET", params); if (json != null) { Log.d("JSON result", json.toString()); return json; } } catch (Exception e) { e.printStackTrace(); } return null; } protected void onPostExecute(JSONObject json) { int success = 0; String message = ""; if (pDialog != null && pDialog.isShowing()) { pDialog.dismiss(); } if (json != null) { Toast.makeText(MainActivity.this, json.toString(), Toast.LENGTH_LONG).show(); try { success = json.getInt(TAG_SUCCESS); message = json.getString(TAG_MESSAGE); } catch (JSONException e) { e.printStackTrace(); } } if (success == 1) { Log.d("Success!", message); }else{ Log.d("Failure", message); } } }
Here are examples of how you would execute the AsyncTasks in this example:
POST version:
String username = "testuser";
String password = "testpass";
new PostAsync().execute(username, password);
GET version:
String username = "testuser";
String password = "testpass";
new GetAsync().execute(username, password);
Here are the simple PHP pages I used for testing the updated JSONParser class and the AsyncTasks.
Note that in order to actually test the login credentials, database calls would need to be added.
POST method version:
<?php // array for JSON response $response = array(); // check for required fields if (isset($_POST['name']) && isset($_POST['password'])) { $name = $_POST['name']; $message = $_POST['message']; $response["success"] = 1; $response["message"] = "Login successful."; // echoing JSON response print(json_encode($response)); } else { // required field is missing $response["success"] = 0; $response["message"] = "Required field(s) missing"; // echoing JSON response print(json_encode($response)); } ?>
GET method version:
<?php // array for JSON response $response = array(); // check for required fields if (isset($_GET['name']) && isset($_GET['password'])) { $name = $_GET['name']; $message = $_GET['message']; $response["success"] = 1; $response["message"] = "Login successful."; // echoing JSON response print(json_encode($response)); } else { // required field is missing $response["success"] = 0; $response["message"] = "Required field(s) missing"; // echoing JSON response print(json_encode($response)); } ?>