Nikhil Kulkarni Nikhil Kulkarni -4 years ago 105
Android Question

i want to make login credentials in generic way

if i do Hard coded i can login but i want to make it generic way,If credentials enter in Login screen,if it matches from Service it has to Login.I'm using in this way

public class LoginScreen extends Activity {

private static final String TAG = "Login Screen";

public static final int CONNECTION_TIMEOUT=100000;
public static final int READ_TIMEOUT=15000;

private EditText editTextUsername;
private EditText editTextPassword;


String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);

editTextUsername = (EditText)findViewById(R.id.emaiId);
editTextPassword = (EditText)findViewById(R.id.password);

}

public void buttonLogin (View arg0)
{
username = editTextUsername.getText().toString();
password = editTextPassword.getText().toString();
new TaskLogin().execute(username,password);
}

public class TaskLogin extends AsyncTask<String,String,String>
{
ProgressDialog pdLoading = new ProgressDialog(LoginScreen.this);
HttpURLConnection conn;
URL url = null;

@Override
protected void onPreExecute() {
super.onPreExecute();

pdLoading.setMessage("\t Loading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {

StringBuilder result = new StringBuilder("unsuccessful");
try{
url = new URL(URLConstants.getLoginURL());

conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/json");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));

writer.write("{\"email\":\"jack@gmail.com\",\"password\":\"12345\"}");
writer.flush();
writer.close();


int response_code = conn.getResponseCode();
System.out.print("Result"+ response_code);
BufferedReader reader = null;

if(response_code==HttpURLConnection.HTTP_OK)
{
InputStream input = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(input));
result = new StringBuilder();
String line;
while ((line= reader.readLine())!=null)
{
result.append(line);
}
System.out.println("result "+result);
}

reader.close();
os.close();
conn.disconnect();
} catch (Exception e1)
{
e1.printStackTrace();
return "Exception";
}
return result.toString();
}

@Override
protected void onPostExecute(String result) {
pdLoading.dismiss();
if(result.equalsIgnoreCase("true"))
{
Intent intent = new Intent(LoginScreen.this,Main2Activity.class);
startActivity(intent);
LoginScreen.this.finish();
}else if (result.equalsIgnoreCase("false"))
{
Toast.makeText(LoginScreen.this, "Invalid email or password", Toast.LENGTH_LONG).show();
} else if(result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful"))
{
Toast.makeText(LoginScreen.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show();
}
}
}


}

My URL consist of Username and password

{"email":"jack@gmail.com","password":"123"}

Answer Source
Use JsonObject like below:

JSONObject job=new JSONObject();
        job.put("email",emailId);
        job.put("pwd",pwd);
writer.write(job.tostring());

where emailId & pwd are variable

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download