From 254eb44a6518c2282896a7f348187e24aaf60111 Mon Sep 17 00:00:00 2001 From: Pratik Rao Date: Thu, 24 Nov 2016 18:51:02 +0530 Subject: [PATCH 1/5] login without session --- app/build.gradle | 3 +- app/src/main/AndroidManifest.xml | 4 + .../com/neosoft/neostore/LoginActivity.java | 92 ++++++++++++++++++- .../com/neosoft/neostore/RegisterAcivity.java | 71 ++++++++++++++ app/src/main/res/layout/activity_login.xml | 16 ++-- 5 files changed, 177 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/com/neosoft/neostore/RegisterAcivity.java diff --git a/app/build.gradle b/app/build.gradle index eee1a05..f654200 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,11 +20,12 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:24.2.1' compile 'com.android.support:design:24.2.1' testCompile 'junit:junit:4.12' + compile 'com.squareup.okhttp3:okhttp:3.4.2' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9a1a399..1770e0a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,9 @@ + + + + diff --git a/app/src/main/java/com/neosoft/neostore/LoginActivity.java b/app/src/main/java/com/neosoft/neostore/LoginActivity.java index adf558c..4cbc06d 100644 --- a/app/src/main/java/com/neosoft/neostore/LoginActivity.java +++ b/app/src/main/java/com/neosoft/neostore/LoginActivity.java @@ -1,12 +1,102 @@ package com.neosoft.neostore; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + +import java.io.IOException; + +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +public class LoginActivity extends AppCompatActivity implements View.OnClickListener { + + Button btnLogin; + TextView txtV; + EditText editEmail,editPass; + String email,pass; + + + public static final String URL_LOGIN = "http://staging.php-dev.in:8844/trainingapp/api/users/login"; -public class LoginActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); + + editEmail = (EditText) findViewById(R.id.edtEmail); + editPass = (EditText) findViewById(R.id.edtPass); + btnLogin = (Button) findViewById(R.id.btnLogin); + + btnLogin.setOnClickListener(this); + + } + + @Override + public void onClick(View view) { + new Login().execute(); + + + } + + public class Login extends AsyncTask + { + + @Override + protected void onPreExecute() { + super.onPreExecute(); + + email = editEmail.getText().toString(); + pass = editPass.getText().toString(); + } + + @Override + protected String doInBackground(String... strings) { + try { + OkHttpClient client = new OkHttpClient(); + + RequestBody requestBody = new FormBody.Builder() + .add("email",email) + .add("password",pass) + .build(); + + Request request = new Request.Builder() + .url(URL_LOGIN) + .post(requestBody) + .build(); + + + Response response = client.newCall(request).execute(); + String res = response.body().string(); + + return res; + + } catch (IOException e) { + e.printStackTrace(); + + } + + return null; + } + + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + Log.d("Response---->>>",s.toString()); + + + } } } diff --git a/app/src/main/java/com/neosoft/neostore/RegisterAcivity.java b/app/src/main/java/com/neosoft/neostore/RegisterAcivity.java new file mode 100644 index 0000000..65f0d2d --- /dev/null +++ b/app/src/main/java/com/neosoft/neostore/RegisterAcivity.java @@ -0,0 +1,71 @@ +package com.neosoft.neostore; + +import android.util.Log; + +import java.io.IOException; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +public class RegisterAcivity { + + public static final String URL_REGISTER = "http://staging.php-dev.in:8844/trainingapp/api/users/register"; + public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); + + OkHttpClient client = new OkHttpClient(); + + public String doGetRequest(String url) throws IOException + { + Request request = new Request.Builder() + .url(url) + .build(); + + Response response = client.newCall(request).execute(); + return response.body().string(); + } + + public String paramsRegister() + { + +// return "{'winCondition':'HIGH_SCORE'," +// + "'name':'Bowling'," +// + "'round':4," +// + "'lastSaved':1367702411696," +// + "'dateStarted':1367702378785," +// + "'players':[" +// + "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39}," +// + "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}" +// + "]}"; + return "{'first_name':'abhijeet1234'," + + "'last_name':'vaidya1234'," + + "'email':'abhijeet@vaidya1234.com'," + + "'password':'abhijeetvaidya1234'," + + "'confirm_password':'abhijeetvaidya1234'," + + "'gender':'M'," + + "'phone_no':9889988998}"; + } + + public RegisterAcivity() { + super(); + } + + public String doPostRequest(String url, String json) throws IOException { + RequestBody requestBody = RequestBody.create(JSON, json); + Request request = new Request.Builder() + .url(url) + .post(requestBody) + .build(); + + Response response = null; + try { + response = client.newCall(request).execute(); + + } catch (IOException e) { + e.printStackTrace(); + } + return response.body().string(); + } +} diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index d1498a0..7ebf63d 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -10,25 +10,27 @@ android:layout_weight="9" android:gravity="center" android:orientation="vertical"> - + + android:hint="Email" + android:inputType="textEmailAddress" />