packageorg.marcinzelent.liberavem;importandroid.Manifest;importandroid.content.Context;importandroid.content.pm.PackageManager;importandroid.location.Location;importandroid.location.LocationListener;importandroid.location.LocationManager;importandroid.os.Bundle;importandroid.provider.ContactsContract;importandroid.support.v4.app.ActivityCompat;importandroid.support.v7.app.AppCompatActivity;importandroid.support.v7.widget.Toolbar;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ArrayAdapter;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.Spinner;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.Calendar;publicclassNewObservationActivityextendsAppCompatActivity{privateBirdselectedBird;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_new_observation);getSupportActionBar().setDisplayHomeAsUpEnabled(true);ButtoncancelButton=findViewById(R.id.new_cancel);cancelButton.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){finish();}});Buttonbutton=findViewById(R.id.new_add);button.setOnClickListener(newButton.OnClickListener(){publicvoidonClick(Viewview){postNewObservation();}});SpinnernewBirdSpinner=findViewById(R.id.new_birds);BirdsListAdapteradapter=newBirdsListAdapter(this,DataKeeper.getInstance().getBirds());newBirdSpinner.setAdapter(adapter);newBirdSpinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener(){@OverridepublicvoidonItemSelected(AdapterView<?>adapterView,Viewview,inti,longl){selectedBird=DataKeeper.getInstance().getBirds()[i];}@OverridepublicvoidonNothingSelected(AdapterView<?>adapterView){}});// Acquire a reference to the system Location ManagerLocationManagerlocationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);// Define a listener that responds to location updatesLocationListenerlocationListener=newLocationListener(){publicvoidonLocationChanged(Locationlocation){// Called when a new location is found by the network location provider.((EditText)findViewById(R.id.new_longitude)).setText(String.valueOf(location.getLongitude()));((EditText)findViewById(R.id.new_latitude)).setText(String.valueOf(location.getLatitude()));}publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){}publicvoidonProviderEnabled(Stringprovider){}publicvoidonProviderDisabled(Stringprovider){}};// Register the listener with the Location Manager to receive location updatesif(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){// TODO: Consider calling// ActivityCompat#requestPermissions// here to request the missing permissions, and then overriding// public void onRequestPermissionsResult(int requestCode, String[] permissions,// int[] grantResults)// to handle the case where the user grants the permission. See the documentation// for ActivityCompat#requestPermissions for more details.return;}locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);}@OverridepublicbooleanonSupportNavigateUp(){finish();returntrue;}publicvoidpostNewObservation(){try{JSONObjectjsonBody=newJSONObject();jsonBody.put("BirdId",selectedBird.getId());jsonBody.put("Comment",((EditText)findViewById(R.id.new_comment)).getText());jsonBody.put("Created","/Date("+System.currentTimeMillis()+"+0000)/");jsonBody.put("Id","0");jsonBody.put("Latitude",((EditText)findViewById(R.id.new_latitude)).getText());jsonBody.put("Longitude",((EditText)findViewById(R.id.new_longitude)).getText());jsonBody.put("Placename",((EditText)findViewById(R.id.new_place)).getText());jsonBody.put("Population",((EditText)findViewById(R.id.new_population)).getText());jsonBody.put("UserId","Sminem");jsonBody.put("NameDanish",selectedBird.getNameDanish());jsonBody.put("NameEnglish",selectedBird.getNameEnglish());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(),"Successfully added activity to the database!",Toast.LENGTH_LONG).show();NewObservationActivity.this.finish();DataKeeper.getInstance().downloadData(NewObservationActivity.this);}},newResponse.ErrorListener(){@OverridepublicvoidonErrorResponse(VolleyErrorerror){Toast.makeText(getBaseContext(),"Couldn't add observation 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();}}}