Starting on an Autenticate activity to obtain access token - the POST parameters are not working yet....
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package uk.org.openseizuredetector;
|
||||
|
||||
//import androidx.appcompat.app.AppCompatActivity;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
public class AuthenticateActivity extends AppCompatActivity {
|
||||
private String TAG = "AuthenticateActivity";
|
||||
private Context mContext;
|
||||
private EditText mUnameEt;
|
||||
private EditText mPasswdEt;
|
||||
private boolean mIsLoggedIn;
|
||||
private WebApiConnection mWac;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
Log.v(TAG, "onCreate()");
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_authenticate);
|
||||
Button cancelBtn =
|
||||
(Button) findViewById(R.id.cancelBtn);
|
||||
cancelBtn.setOnClickListener(onCancel);
|
||||
Button OKBtn = (Button) findViewById(R.id.OKBtn);
|
||||
OKBtn.setOnClickListener(onOK);
|
||||
|
||||
mUnameEt = (EditText) findViewById(R.id.username);
|
||||
mPasswdEt = (EditText) findViewById(R.id.password);
|
||||
mWac = new WebApiConnection(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
switchUi();
|
||||
}
|
||||
|
||||
private void switchUi() {
|
||||
SharedPreferences prefs;
|
||||
String storedAuthToken;
|
||||
LinearLayout loginLl = (LinearLayout)findViewById(R.id.login_ui);
|
||||
LinearLayout logoutLl = (LinearLayout)findViewById(R.id.logout_ui);
|
||||
Log.i(TAG, "switchUi()");
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
storedAuthToken = (prefs.getString("webApiAuthToken", null));
|
||||
Log.v(TAG, "storedAuthToken=" + storedAuthToken);
|
||||
|
||||
// Check if we are already logged in
|
||||
if (storedAuthToken == null || storedAuthToken.length() == 0) {
|
||||
Log.v(TAG, "Not Logged in - showing log in UI");
|
||||
loginLl.setVisibility(View.VISIBLE);
|
||||
logoutLl.setVisibility(View.GONE);
|
||||
} else {
|
||||
Log.v(TAG, "Already Logged in - showing Log Out prompt");
|
||||
loginLl.setVisibility(View.GONE);
|
||||
logoutLl.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
View.OnClickListener onCancel =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onCancel");
|
||||
//m_status=false;
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
View.OnClickListener onOK =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
//m_status=true;
|
||||
Log.v(TAG, "onOK()");
|
||||
String uname = mUnameEt.getText().toString();
|
||||
String passwd = mPasswdEt.getText().toString();
|
||||
Log.v(TAG,"onOK() - uname="+uname+", passwd="+passwd);
|
||||
mWac.authenticate(uname,passwd);
|
||||
//finish();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@@ -244,6 +244,17 @@ public class MainActivity extends AppCompatActivity {
|
||||
mConnection.mSdServer.sendPhoneAlarm();
|
||||
}
|
||||
return true;
|
||||
case R.id.action_authenticate_api:
|
||||
Log.i(TAG, "action_autheticate_api");
|
||||
try {
|
||||
Intent i = new Intent(
|
||||
MainActivity.this,
|
||||
AuthenticateActivity.class);
|
||||
this.startActivity(i);
|
||||
} catch (Exception ex) {
|
||||
Log.i(TAG, "exception starting export activity " + ex.toString());
|
||||
}
|
||||
return true;
|
||||
case R.id.action_export:
|
||||
Log.i(TAG, "action_export");
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package uk.org.openseizuredetector;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.RequestQueue;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.android.volley.toolbox.JsonObjectRequest;
|
||||
import com.android.volley.toolbox.StringRequest;
|
||||
import com.android.volley.toolbox.Volley;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
// This class is intended to handle all interactions with the OSD WebAPI
|
||||
public class WebApiConnection {
|
||||
public String retVal;
|
||||
public int retCode;
|
||||
private String mUrlBase = "https://osdApi.ddns.net";
|
||||
private String TAG = "WebApiConnection";
|
||||
RequestQueue mQueue;
|
||||
|
||||
public WebApiConnection(Context context) {
|
||||
mQueue = Volley.newRequestQueue(context);
|
||||
}
|
||||
|
||||
public boolean authenticate(String uname, String passwd) {
|
||||
// We know that this command works, so we just need the Java equivalent:
|
||||
// curl -X POST -d 'login=graham4&password=testpwd1' https://osdapi.ddns.net/api/accounts/login/
|
||||
String urlStr = mUrlBase + "/api/accounts/login/";
|
||||
Log.v(TAG, "urlStr=" + urlStr);
|
||||
|
||||
JSONObject postData = new JSONObject();
|
||||
try {
|
||||
postData.put("login", uname);
|
||||
postData.put("password", passwd);
|
||||
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG,e.getMessage());
|
||||
}
|
||||
|
||||
Log.v(TAG,postData.toString());
|
||||
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, urlStr, postData,
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
// Display the first 500 characters of the response string.
|
||||
Log.v(TAG, "Response is: " + response);
|
||||
}
|
||||
},
|
||||
new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
String responseBody = new String(error.networkResponse.data);
|
||||
Log.v(TAG, "Login Error" + error.toString()+","+error.getMessage()+error.networkResponse.statusCode+","+responseBody);
|
||||
}
|
||||
}
|
||||
);
|
||||
mQueue.add(req);
|
||||
return (true);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user