implement list of rented bikes
This commit is contained in:
parent
01ad943822
commit
6ff1f0e0c4
3 changed files with 88 additions and 21 deletions
|
|
@ -8,11 +8,22 @@ import android.support.design.widget.FloatingActionButton;
|
|||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements AsyncTaskCallbacks<String> {
|
||||
private RequestHandler getBikesTask = null;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
|
@ -45,6 +56,9 @@ public class MainActivity extends AppCompatActivity {
|
|||
Intent intent = new Intent(this, LoginActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
else {
|
||||
reloadBikeList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -69,4 +83,55 @@ public class MainActivity extends AppCompatActivity {
|
|||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
protected void reloadBikeList() {
|
||||
//get loginkey
|
||||
SharedPreferences sharedPref = getSharedPreferences("persistence", MODE_PRIVATE);
|
||||
String defaultValue = "nokey";
|
||||
String loginKey = sharedPref.getString("loginKey", defaultValue);
|
||||
|
||||
String[] params = {
|
||||
"apikey=", getString(R.string.apikey),
|
||||
"loginkey=", loginKey
|
||||
};
|
||||
|
||||
getBikesTask = new RequestHandler(this, "POST",
|
||||
"api/getOpenRentals.json", params);
|
||||
getBikesTask.execute((Void) null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTaskComplete(String response) {
|
||||
if (!response.isEmpty()) {
|
||||
final ArrayList<String> list = new ArrayList<>();
|
||||
try {
|
||||
JSONObject jObject = new JSONObject(response);
|
||||
JSONArray bikesArray = jObject.getJSONArray("rentalCollection");
|
||||
|
||||
for (int i = 0; i < bikesArray.length(); i++) {
|
||||
String entry;
|
||||
JSONObject bike = bikesArray.getJSONObject(i);
|
||||
entry = "Bike " + bike.getString("bike")
|
||||
+ " with lock code " + bike.getString("code");
|
||||
list.add(entry);
|
||||
}
|
||||
Log.d("DEBUG", list.toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
final ListView listview = findViewById(R.id.listview);
|
||||
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
|
||||
android.R.layout.simple_list_item_1, list);
|
||||
listview.setAdapter(adapter);
|
||||
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
|
||||
//TODO: Return bike
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
else {
|
||||
//TODO: implement error handling
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,10 +15,14 @@ import java.net.URLEncoder;
|
|||
|
||||
public class RequestHandler extends AsyncTask<Void, Void, String> {
|
||||
|
||||
private String mHTTPmethod;
|
||||
private String mEndpoint;
|
||||
private AsyncTaskCallbacks<String> callback;
|
||||
private String[] mCredentials;
|
||||
|
||||
RequestHandler(String[] credentials, AsyncTaskCallbacks<String> act) {
|
||||
RequestHandler(AsyncTaskCallbacks<String> act, String HTTPmethod, String endpoint, String[] credentials) {
|
||||
mHTTPmethod = HTTPmethod;
|
||||
mEndpoint = endpoint;
|
||||
mCredentials = credentials;
|
||||
callback = act;
|
||||
}
|
||||
|
|
@ -36,9 +40,10 @@ public class RequestHandler extends AsyncTask<Void, Void, String> {
|
|||
HttpURLConnection connection = null;
|
||||
try {
|
||||
//Create connection
|
||||
URL url = new URL("https://api.nextbike.net/api/login.json");
|
||||
URL url = new URL("https://api.nextbike.net/" + mEndpoint);
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestMethod(mHTTPmethod);
|
||||
if(mHTTPmethod.equals("POST")) {
|
||||
connection.setRequestProperty("Content-Type",
|
||||
"application/x-www-form-urlencoded");
|
||||
|
||||
|
|
@ -46,10 +51,10 @@ public class RequestHandler extends AsyncTask<Void, Void, String> {
|
|||
Integer.toString(urlParameters.toString().getBytes().length));
|
||||
connection.setRequestProperty("Content-Language", "en-US");
|
||||
|
||||
connection.setUseCaches (false);
|
||||
connection.setUseCaches(false);
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
|
||||
}
|
||||
//Send request
|
||||
DataOutputStream wr = new DataOutputStream (
|
||||
connection.getOutputStream ());
|
||||
|
|
|
|||
|
|
@ -8,13 +8,10 @@
|
|||
tools:context=".MainActivity"
|
||||
tools:showIn="@layout/activity_main">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/listview"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
|
||||
</android.support.constraint.ConstraintLayout>
|
||||
Loading…
Add table
Add a link
Reference in a new issue