-2

Hey guys i am new to Android networking concepts.I want to send username,password,imei number and location to the php server from android app.I am done my sending part.now my question is how to receive the response.i want to get the status (1 or 0) according to that i want to move to the next page.so anyone will know how to do this you are welcome.

       private static final String REGISTER_URL="http://vPC70.com/App/login.php";
     username =  editTextUserName.getText().toString().toLowerCase();
     userpassword=editTextPassword.getText().toString().toLowerCase();
     loc="11.295756,77.001890";
      imeino = "12312312456";
     register(username, userpassword, imeino, loc);

     private void register(final String username, final String userpassword, 
      String imeino, String loc) {
      String urlSuffix = "?
      username="+username+"&userpassword="+userpassword+"&imeino="+imeino
     +"&location="+loc;
      class RegisterUser extends AsyncTask<String,String , String>{

        ProgressDialog loading;


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(LoginActivity.this, "Please 
       Wait",null, true, true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();

            }

        @Override
        protected String doInBackground(String... params) {
            String s = params[0];
            BufferedReader bufferedReader = null;
            try {
                URL url = new URL(REGISTER_URL+s);
                HttpURLConnection con = (HttpURLConnection) 
           url.openConnection();
                bufferedReader = new BufferedReader(new 
           InputStreamReader(con.getInputStream()));

                String result;

                result = bufferedReader.readLine();
                return result;

            }catch(Exception e){
                return null;
            }

        }
      }
      RegisterUser ru = new RegisterUser();
      ru.execute(urlSuffix);

this is the response

      {"Login":[{"status":"1","message":"Login Successfully !!!"}]}
      {"Login":[{"status":"0","message":"Invalid Password !!!"}]}

if the response is 1 toast the message login sucessfully if the response is 0 toast the message invalid password in post execute

karthi keyan
  • 83
  • 1
  • 11

5 Answers5

0

In onPostExecute(String s) you can convert result into json and check status value like

 @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        loading.dismiss();
        JsonObject object = new JsonObject(s);
        if(object.optString("status").equals("1"))
          {
          // Your Logic here
         }

     }
Anil
  • 1,087
  • 1
  • 11
  • 24
0

Create the POJO / Model class to convert your Response.

Like this

public class LoginResponse{

@SerializedName("Login")
@Expose
private List<Login> login = null;

public List<Login> getLogin() {
return login;
}

public void setLogin(List<Login> login) {
this.login = login;
}

}

public class Login {

@SerializedName("status")
@Expose
private String status;
@SerializedName("message")
@Expose
private String message;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}

in PostExcecute convert the response to POJO object like this by using GSON

 Gson gson = new Gson();
 LoginResponse response = gson.toJson(result, LoginResponse.class);

Here you can check the conditions:;

if(response !=null && response.getLogin() !=null)
{
    if(response.getLogin().getStatus().equalIgnoreCase("1"))
     {
              // show toast Login Successfully !!! and move to next screen

     }
else if(response.getLogin().getStatus().equalIgnoreCase("0"))
    {
     // Invalid Password !!! your logic here
    }
 }
King of Masses
  • 18,405
  • 4
  • 60
  • 77
0

Here is the parser according to your response string

 private void parseResponseJson(String response) throws JSONException {
    JSONObject jsonObject = new JSONObject(response).getJSONArray("Login").getJSONObject(0);
    String status = jsonObject.getString("status");
    String message = jsonObject.getString("message");
}
Hamid Reza
  • 624
  • 7
  • 23
0

After getting the response from server,based on status display the message in toast

try {
                    JSONObject jobj = new JSONObject(response);


                    String status = jobj.getString("status");

                    String msg = jobj.getString("message");

                    if (status.equals("1")) {
                        //move to next page
                        Toast.makeText(LoginActivity.this, msg,Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(LoginActivity.this, msg,Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
Sunil P
  • 3,698
  • 3
  • 13
  • 20
0

Simple and Efficient solution. Use google's Gson library . You can easily create a hashmap from json string like this.

Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson(JSONString, type);
Natesh bhat
  • 12,274
  • 10
  • 84
  • 125