diff options
author | Marcin Zelent <zelent.marcin@gmail.com> | 2018-04-20 16:34:25 +0200 |
---|---|---|
committer | Marcin Zelent <zelent.marcin@gmail.com> | 2018-04-20 16:34:25 +0200 |
commit | 2cf7ca569e50caa8d33d4045977d69d1f10771c2 (patch) | |
tree | 2339f4725e67b8c3fa4b68315d690084cd500fd0 /app/src/main/java | |
parent | e4fac53bb0e2a5234116caf8651e8d2c5a38a4f1 (diff) |
Added partially posting
Diffstat (limited to 'app/src/main/java')
5 files changed, 192 insertions, 30 deletions
diff --git a/app/src/main/java/org/marcinzelent/liberavem/BirdPickerActivity.java b/app/src/main/java/org/marcinzelent/liberavem/BirdPickerActivity.java new file mode 100644 index 0000000..15c38cf --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/BirdPickerActivity.java @@ -0,0 +1,13 @@ +package org.marcinzelent.liberavem; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; + +public class BirdPickerActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_bird_picker); + } +} diff --git a/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java b/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java index 67eba43..27f9dc4 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java +++ b/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java @@ -106,7 +106,7 @@ public class DataKeeper { MyObservationsFragment mo = (MyObservationsFragment) ((MainActivity)activity).getAllFragments().get(0); List<Observation> myObservationsList = new ArrayList<>(); for (Observation o : observations) - if (o.getUserId().equals("Nancy")) myObservationsList.add(o); + if (o.getUserId().equals("Sminem")) myObservationsList.add(o); Observation[] myObservations = new Observation[myObservationsList.size()]; myObservations = myObservationsList.toArray(myObservations); diff --git a/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java b/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java new file mode 100644 index 0000000..cf1cf18 --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/NewObservationActivity.java @@ -0,0 +1,111 @@ +package org.marcinzelent.liberavem; + +import android.app.Activity; +import android.os.Bundle; +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.widget.Button; +import android.widget.Toast; + +import com.android.volley.AuthFailureError; +import com.android.volley.NetworkResponse; +import com.android.volley.Request; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.VolleyLog; +import com.android.volley.toolbox.HttpHeaderParser; +import com.android.volley.toolbox.StringRequest; +import com.android.volley.toolbox.Volley; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.UnsupportedEncodingException; +import java.util.HashMap; +import java.util.Map; + +public class NewObservationActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_new_observation); + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + Button button = findViewById(R.id.button); + button.setOnClickListener(new Button.OnClickListener() { + public void onClick(View view) { + postNewObservation(); + } + }); + } + + public void postNewObservation() { + try { + JSONObject jsonBody = new JSONObject(); + jsonBody.put("BirdId", "51"); + jsonBody.put("Comment", "Værsgo, my friend"); + jsonBody.put("Created", "/Date(1524182400000+0000)/"); + jsonBody.put("Id", "162"); + jsonBody.put("Latitude", "30.1187718"); + jsonBody.put("Longitude", "51.381484"); + jsonBody.put("Placename", "Black lodge"); + jsonBody.put("Population", "5"); + jsonBody.put("UserId", "Sminem"); + jsonBody.put("NameDanish", "gråkrage"); + jsonBody.put("NameEnglish", "hooded crow"); + final String requestBody = jsonBody.toString(); + + String url = "http://birdobservationservice.azurewebsites.net/Service1.svc/observations"; + StringRequest request = new StringRequest(Request.Method.POST, url, + new Response.Listener<String>() { + @Override + public void onResponse(String response) { + Toast.makeText(getBaseContext(), "Couldn't add observation to the database!", Toast.LENGTH_LONG).show(); + } + }, + new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + Toast.makeText(getBaseContext(), "Successfully added activity to the database!", Toast.LENGTH_LONG).show(); + } + }) { + + @Override + public String getBodyContentType() { + return "application/json; charset=utf-8"; + } + + @Override + public byte[] getBody() throws AuthFailureError { + try { + return requestBody == null ? null : requestBody.getBytes("utf-8"); + } catch (UnsupportedEncodingException uee) { + VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8"); + return null; + } + } + + @Override + protected Response<String> parseNetworkResponse(NetworkResponse response) { + String responseString = ""; + if (response != null) { + responseString = String.valueOf(response.statusCode); + // can get more details such as response.headers + } + return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response)); + } + }; + + Volley.newRequestQueue(getBaseContext()).add(request); + } + catch (JSONException e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/main/java/org/marcinzelent/liberavem/Observation.java b/app/src/main/java/org/marcinzelent/liberavem/Observation.java index be02812..1aca80b 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/Observation.java +++ b/app/src/main/java/org/marcinzelent/liberavem/Observation.java @@ -5,89 +5,126 @@ import com.google.gson.annotations.SerializedName; import java.io.Serializable; public class Observation implements Serializable { - @SerializedName("Id") - private int id; + @SerializedName("BirdId") + private int birdId; + @SerializedName("Comment") + private String comment; @SerializedName("Created") private String created; + @SerializedName("Id") + private int id; @SerializedName("Latitude") private double latitude; @SerializedName("Longitude") private double longitude; @SerializedName("Placename") private String placeName; - @SerializedName("Comment") - private String comment; + @SerializedName("Population") + private int population; @SerializedName("UserId") private String userId; - @SerializedName("BirdId") - private int birdId; - @SerializedName("NameEnglish") - private String nameEnglish; @SerializedName("NameDanish") private String nameDanish; - @SerializedName("Population") - private int population; + @SerializedName("NameEnglish") + private String nameEnglish; public Observation(int id, String created, double latitude, double longitude, String placeName, String comment, String userId, int birdId, String nameEnglish, String nameDanish, int population) { - this.id = id; + this.birdId = birdId; + this.comment = comment; this.created = created; + this.id = id; this.latitude = latitude; this.longitude = longitude; this.placeName = placeName; - this.comment = comment; + this.population = population; this.userId = userId; - this.birdId = birdId; - this.nameEnglish = nameEnglish; this.nameDanish = nameDanish; - this.population = population; + this.nameEnglish = nameEnglish; } - public int getId() { - return id; + public int getBirdId() { + return birdId; + } + + public void setBirdId(int birdId) { + this.birdId = birdId; + } + + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; } public String getCreated() { return created; } + public void setCreated(String created) { this.created = created; } + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + public double getLatitude() { return latitude; } + public void setLatitude(double latitude) { + this.latitude = latitude; + } + public double getLongitude() { return longitude; } + public void setLongitude(double longitude) { + this.longitude = longitude; + } + public String getPlaceName() { return placeName; } - public String getComment() { - return comment; + public void setPlaceName(String placeName) { + this.placeName = placeName; } - public String getUserId() { - return userId; + public int getPopulation() { + return population; } - public int getBirdId() { - return birdId; + public void setPopulation(int population) { + this.population = population; } - public String getNameEnglish() { - return nameEnglish; + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; } public String getNameDanish() { return nameDanish; } - public int getPopulation() { - return population; + public String getNameEnglish() { + return nameEnglish; + } + + public void setNameEnglish(String nameEnglish) { + this.nameEnglish = nameEnglish; } } diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java index b093461..128cbe9 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java @@ -1,6 +1,7 @@ package org.marcinzelent.liberavem; import android.content.Context; +import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; @@ -98,8 +99,8 @@ public class ObservationsFragment extends Fragment { fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); + Intent intent = new Intent(getActivity(), NewObservationActivity.class); + startActivity(intent); } }); } |