Started adding code to create a datapoint - getting error 405 "POST Not Allowed", which might be an issue with the API
This commit is contained in:
@@ -14,9 +14,13 @@ import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class AuthenticateActivity extends AppCompatActivity implements AuthCallbackInterface {
|
||||
public class AuthenticateActivity extends AppCompatActivity
|
||||
implements AuthCallbackInterface, EventCallbackInterface, DatapointCallbackInterface {
|
||||
private String TAG = "AuthenticateActivity";
|
||||
private Context mContext;
|
||||
private EditText mUnameEt;
|
||||
@@ -40,9 +44,14 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
|
||||
Button logoutBtn = (Button)findViewById(R.id.logoutBtn);
|
||||
logoutBtn.setOnClickListener(onLogout);
|
||||
|
||||
Button createEventBtn = (Button)findViewById(R.id.createEventBtn);
|
||||
createEventBtn.setOnClickListener(onCreateEvent);
|
||||
Button createDatapointBtn = (Button)findViewById(R.id.createDatapointBtn);
|
||||
createDatapointBtn.setOnClickListener(onCreateDatapoint);
|
||||
|
||||
mUnameEt = (EditText) findViewById(R.id.username);
|
||||
mPasswdEt = (EditText) findViewById(R.id.password);
|
||||
mWac = new WebApiConnection(this, this);
|
||||
mWac = new WebApiConnection(this, this, this, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -54,7 +63,14 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
|
||||
public void authCallback(boolean authSuccess, String tokenStr) {
|
||||
Log.v(TAG,"authCallback");
|
||||
updateUi();
|
||||
mWac.createEvent(10,new Date(),"eventDescription....");
|
||||
}
|
||||
|
||||
public void eventCallback(boolean success, String eventStr) {
|
||||
Log.v(TAG,"eventCallback");
|
||||
}
|
||||
|
||||
public void datapointCallback(boolean success, String datapointStr) {
|
||||
Log.v(TAG,"datapointCallback");
|
||||
}
|
||||
|
||||
private void updateUi() {
|
||||
@@ -117,5 +133,36 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb
|
||||
updateUi();
|
||||
}
|
||||
};
|
||||
View.OnClickListener onCreateEvent =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onCreateEvent");
|
||||
mWac.createEvent(10,new Date(),"eventDescription....");
|
||||
}
|
||||
};
|
||||
|
||||
View.OnClickListener onCreateDatapoint =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onCreateDatapoint");
|
||||
String jsonStr = "";
|
||||
JSONObject dataObj;
|
||||
try {
|
||||
jsonStr = "{HR:70}";
|
||||
dataObj = new JSONObject(jsonStr);
|
||||
Log.v(TAG, "Creating Datapoint..."+dataObj.toString());
|
||||
mWac.createDatapoint(dataObj,10);
|
||||
} catch (JSONException e) {
|
||||
Log.v(TAG,"Error Creating JSON Object from string "+jsonStr);
|
||||
dataObj = null;
|
||||
jsonStr = null;
|
||||
}
|
||||
//m_status=false;
|
||||
// mWac.logout();
|
||||
//updateUi();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package uk.org.openseizuredetector;
|
||||
|
||||
public interface DatapointCallbackInterface {
|
||||
// Interface is called when a new datapoint is created in the database.
|
||||
void datapointCallback(boolean success, String eventStr);
|
||||
}
|
||||
|
||||
@@ -36,14 +36,17 @@ public class WebApiConnection {
|
||||
private String TAG = "WebApiConnection";
|
||||
private AuthCallbackInterface mAuthCallback;
|
||||
private EventCallbackInterface mEventCallback;
|
||||
private DatapointCallbackInterface mDatapointCallback;
|
||||
private Context mContext;
|
||||
private String TOKEN_ID = "webApiAuthToken";
|
||||
RequestQueue mQueue;
|
||||
|
||||
public WebApiConnection(Context context, AuthCallbackInterface authCallback, EventCallbackInterface eventCallback) {
|
||||
public WebApiConnection(Context context, AuthCallbackInterface authCallback, EventCallbackInterface eventCallback,
|
||||
DatapointCallbackInterface datapointCallback) {
|
||||
mContext = context;
|
||||
mAuthCallback = authCallback;
|
||||
mEventCallback = eventCallback;
|
||||
mDatapointCallback = datapointCallback;
|
||||
mQueue = Volley.newRequestQueue(context);
|
||||
}
|
||||
|
||||
@@ -182,6 +185,7 @@ public class WebApiConnection {
|
||||
//params.put("desc", eventDesc);
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getHeaders() throws AuthFailureError {
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
@@ -189,6 +193,84 @@ public class WebApiConnection {
|
||||
params.put("Authorization", "Token " + getStoredToken());
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBody() throws AuthFailureError {
|
||||
try {
|
||||
return dataStr == null ? null : dataStr.getBytes("utf-8");
|
||||
} catch (UnsupportedEncodingException uee) {
|
||||
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", dataStr, "utf-8");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
mQueue.add(req);
|
||||
return (true);
|
||||
}
|
||||
|
||||
public boolean createDatapoint(JSONObject dataObj, int eventId) {
|
||||
Log.v(TAG, "createDatapoint() - FIXME - This does not do anything!");
|
||||
// Create a new event in the remote database, based on the provided parameters.
|
||||
String urlStr = mUrlBase + "/api/datapoints/";
|
||||
Log.v(TAG, "urlStr=" + urlStr);
|
||||
final String authtoken = getStoredToken();
|
||||
|
||||
if (!isLoggedIn()) {
|
||||
Log.v(TAG, "not logged in - doing nothing");
|
||||
return (false);
|
||||
}
|
||||
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
try {
|
||||
jsonObject.put("eventId", String.valueOf(eventId));
|
||||
jsonObject.put("dataJSON", dataObj.toString());
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "Error generating event JSON string");
|
||||
}
|
||||
final String dataStr = jsonObject.toString();
|
||||
Log.v(TAG, "createDatapoint - data=" + dataStr);
|
||||
|
||||
|
||||
StringRequest req = new StringRequest(Request.Method.POST, urlStr,
|
||||
new Response.Listener<String>() {
|
||||
@Override
|
||||
public void onResponse(String response) {
|
||||
Log.v(TAG, "Response is: " + response);
|
||||
mDatapointCallback.datapointCallback(true, response);
|
||||
}
|
||||
},
|
||||
new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
String responseBody = new String(error.networkResponse.data);
|
||||
Log.v(TAG, "Create Datapoint Error: " + error.toString() + ", message:" + error.getMessage() + ", Response Code:" + error.networkResponse.statusCode + ", Response: " + responseBody);
|
||||
mDatapointCallback.datapointCallback(false, new String(error.networkResponse.data));
|
||||
}
|
||||
}) {
|
||||
// Note, this is overriding part of StringRequest, not one of the sub-classes above!
|
||||
@Override
|
||||
protected Map<String, String> getParams() {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
// params.put("name",sname); // passing parameters to server
|
||||
String authToken = getStoredToken();
|
||||
params.put("Authorization: Token " + authToken, authToken);
|
||||
Log.v(TAG, "getParams: params=" + params.toString());
|
||||
//params.put("eventType", String.valueOf(eventType));
|
||||
//params.put("dataTime", dateFormat.format(eventDate));
|
||||
//params.put("desc", eventDesc);
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getHeaders() throws AuthFailureError {
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("Content-Type", "application/json; charset=UTF-8");
|
||||
params.put("Authorization", "Token " + getStoredToken());
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBody() throws AuthFailureError {
|
||||
try {
|
||||
@@ -203,16 +285,6 @@ public class WebApiConnection {
|
||||
mQueue.add(req);
|
||||
return (true);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public boolean createDatapoint() {
|
||||
Log.v(TAG,"createDatapoint() - FIXME - This does not do anything!");
|
||||
return(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -93,6 +93,26 @@
|
||||
android:layout_weight="1"
|
||||
android:text="@string/logout" />
|
||||
</LinearLayout>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<Button
|
||||
android:id="@+id/createEventBtn"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="Create Event" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/createDatapointBtn"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="Create Datapoint" />
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user