0

I'm new to Android and Web Services in java. I'm trying to pass a Java object or class as a parameter to a method in the web service. I'm using an android application as the client and everything works fine when ever I'm passing a string using but now I want to pass a String variable that contains a slash '/' so it doesn't seem to be working. So I thought that the best option was to pass in an Object as a parameter so I could get the values in whatever format they are from the webservice. Please how to I write the server side and client side codes. Here is my previous code for my web service

@Path("/Student")

public class StudentController {

StudentService studentService;

@GET
@Path("/{username},{password}")
@Produces(MediaType.APPLICATION_JSON)
public Student authenticate(@PathParam("username") String username, @PathParam("password") String password) {

    StringTokenizer st = new StringTokenizer(username, "/");
    String school = st.nextToken();
    studentService = new StudentService("Laurels");

    return studentService.validateStudent("Laurels/2015/"+username, "Laurels/2015/"+password);
}

}

Here is my previous code for my android client

private class Authenticate extends AsyncTask<Void, Void, Void>{

    Snackbar snackbar = Snackbar.make(btnLogin, "Result", Snackbar.LENGTH_LONG);

    private int status;
    private Bundle bundle;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        String url = "http://192.168.42.209:8085/SchoolWebService/rest/Student/"+username+","+password;
        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject c = new JSONObject(jsonStr);

                String uname = c.getString("username");
                String pword = c.getString("password");
                String surname = c.getString("surname");
                boolean validated = c.getBoolean("validated");
                if(!validated){
                    snackbar.setText("Invalid Login").show();
                    status = 0;
                }else{
                    snackbar.setDuration(Snackbar.LENGTH_SHORT).setText("Authenticated!").show();
                    status = 1;
                    bundle = new Bundle();
                    bundle.putString("username", username);
                    bundle.putString("password", password);
                    bundle.putBoolean("validated", validated);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            snackbar.setText("Connection Failed!").show();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }
        if(status == 1){
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
            finish();
        }
    }
}
Charles
  • 92
  • 10
  • Are you passing around passwords in plaintext? I assume this is just a learning exercise? – OneCricketeer Aug 17 '16 at 12:52
  • Here's an example without an AsyncTask, since that's probably making things more difficult for you. http://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley – OneCricketeer Aug 17 '16 at 12:56
  • Yeah. It's a learning exercise @cricket_007. But how can I secure the password? – Charles Aug 17 '16 at 13:02
  • I've looked at the example in your link and I can't seem to understand it since I don't know how to use volley. I use apache http api. – Charles Aug 17 '16 at 13:05
  • That's a broad topic in itself and the best ways have changed over the years. Here's a video about [how NOT to store a password](https://youtu.be/8ZtInClXe1Q) – OneCricketeer Aug 17 '16 at 13:05
  • Ok. My main issue is what I stated. Can't seem to find a way – Charles Aug 17 '16 at 13:06
  • Apache HTTP is deprecated on Android, but the concept is the same. Whatever library you use, you need to convert the Java object to JSON and make a POST request with that object on the client. Then, the server must accept and parse out the JSON – OneCricketeer Aug 17 '16 at 13:07
  • If I had any idea on how to do that I won't have posted this question. Like I said I'm new to all these – Charles Aug 17 '16 at 13:14
  • And another one. http://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android – OneCricketeer Aug 17 '16 at 13:17

0 Answers0