Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.arunraj.locationtracker;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- public class MainActivity extends ActionBarActivity {
- String L_URL= "http://safetracker-threetinker.rhcloud.com/api/login";
- EditText e_user,e_pwd;
- String c_username,c_password;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- e_user = (EditText) findViewById(R.id.e_user);
- e_pwd = (EditText) findViewById(R.id.e_pwd);
- Button b_login = (Button) findViewById(R.id.b_login);
- Button b_register = (Button) findViewById(R.id.b_register);
- b_login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- c_username=e_user.getText().toString();
- c_password=e_pwd.getText().toString();
- LoginAsyn loginAsyn=new LoginAsyn();
- loginAsyn.execute(L_URL);
- //
- }
- });
- b_register.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
- startActivity(i);
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it i present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- class LoginAsyn extends AsyncTask<String,Void,String>
- {
- private ProgressDialog nDialog;
- @Override
- protected void onPreExecute()
- {
- //progress dialog
- nDialog = new ProgressDialog(MainActivity.this);
- nDialog.setMessage("Loading..");
- nDialog.setTitle("Login....");
- nDialog.setIndeterminate(false);
- nDialog.setCancelable(true);
- nDialog.show();
- }
- @Override
- protected String doInBackground(String... params) {
- InputStream inputStream = null;
- String result = "";
- try {
- // 1. create HttpClient
- HttpClient httpclient = new DefaultHttpClient();
- // 2. make POST request to the given URL
- HttpPost httpPost = new HttpPost(params[0]);
- String json = "";
- // 3. build jsonObject
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("name", c_username);
- jsonObject.put("password", c_password);
- // 4. convert JSONObject to JSON to String
- json = jsonObject.toString();
- // 5. set json to StringEntity
- StringEntity se = new StringEntity(json);
- // 6. set httpPost Entity
- httpPost.setEntity(se);
- // 7. Set some headers to inform server about the type of the content
- httpPost.setHeader("Accept", "application/json");
- httpPost.setHeader("Content-type", "application/json");
- // 8. Execute POST request to the given URL
- HttpResponse httpResponse = httpclient.execute(httpPost);
- inputStream = httpResponse.getEntity().getContent();
- BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
- String line = "";
- while((line = bufferedReader.readLine()) != null)
- result += line;
- inputStream.close();
- } catch (Exception e) {
- }
- // 11. return result
- return result;
- }
- @Override
- protected void onPostExecute(String s) {
- super.onPostExecute(s);
- if (nDialog.isShowing()) {
- nDialog.dismiss();
- }
- Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement