packageorg.marcinzelent.liberavem;importandroid.app.Activity;importandroid.os.Bundle;importandroid.support.design.widget.FloatingActionButton;importandroid.support.design.widget.Snackbar;importandroid.support.v7.app.AppCompatActivity;importandroid.support.v7.widget.Toolbar;importandroid.util.Log;importandroid.view.View;importandroid.widget.Button;importandroid.widget.Toast;importcom.android.volley.AuthFailureError;importcom.android.volley.NetworkResponse;importcom.android.volley.Request;importcom.android.volley.Response;importcom.android.volley.VolleyError;importcom.android.volley.VolleyLog;importcom.android.volley.toolbox.HttpHeaderParser;importcom.android.volley.toolbox.StringRequest;importcom.android.volley.toolbox.Volley;importorg.json.JSONException;importorg.json.JSONObject;importjava.io.UnsupportedEncodingException;importjava.util.HashMap;importjava.util.Map;publicclassNewObservationActivityextendsAppCompatActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_new_observation);Toolbartoolbar=(Toolbar)findViewById(R.id.toolbar);setSupportActionBar(toolbar);Buttonbutton=findViewById(R.id.button);button.setOnClickListener(newButton.OnClickListener(){publicvoidonClick(Viewview){postNewObservation();}});}publicvoidpostNewObservation(){try{JSONObjectjsonBody=newJSONObject();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");finalStringrequestBody=jsonBody.toString();Stringurl="http://birdobservationservice.azurewebsites.net/Service1.svc/observations";StringRequestrequest=newStringRequest(Request.Method.POST,url,newResponse.Listener<String>(){@OverridepublicvoidonResponse(Stringresponse){Toast.makeText(getBaseContext(),"Couldn't add observation to the database!",Toast.LENGTH_LONG).show();}},newResponse.ErrorListener(){@OverridepublicvoidonErrorResponse(VolleyErrorerror){Toast.makeText(getBaseContext(),"Successfully added activity to the database!",Toast.LENGTH_LONG).show();}}){@OverridepublicStringgetBodyContentType(){return"application/json; charset=utf-8";}@Overridepublicbyte[]getBody()throwsAuthFailureError{try{returnrequestBody==null?null:requestBody.getBytes("utf-8");}catch(UnsupportedEncodingExceptionuee){VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",requestBody,"utf-8");returnnull;}}@OverrideprotectedResponse<String>parseNetworkResponse(NetworkResponseresponse){StringresponseString="";if(response!=null){responseString=String.valueOf(response.statusCode);// can get more details such as response.headers}returnResponse.success(responseString,HttpHeaderParser.parseCacheHeaders(response));}};Volley.newRequestQueue(getBaseContext()).add(request);}catch(JSONExceptione){e.printStackTrace();}}}