Java tutorial
/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cz.cvut.via.androidrestskeleton; import java.util.Collections; import java.util.HashMap; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.web.client.RestTemplate; import android.app.Activity; import android.os.Bundle; import android.os.StrictMode; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckedTextView; import cz.cvut.via.androidrestskeleton.asyncrq.AsyncDELETE; import cz.cvut.via.androidrestskeleton.asyncrq.AsyncGET; import cz.cvut.via.androidrestskeleton.asyncrq.AsyncPOST; import cz.cvut.via.androidrestskeleton.asyncrq.AsyncPOST_QUERY; import cz.cvut.via.androidrestskeleton.asyncrq.AsyncPUT; import cz.cvut.via.androidrestskeleton.asyncrq.Response; import cz.cvut.via.androidrestskeleton.model.PostalAddress; import cz.cvut.via.androidrestskeleton.model.RESTQuery; /** * This class provides a basic demonstration of how to write an Android * activity. Inside of its window, it places a single view: an EditText that * displays and edits some internal text. */ public class SkeletonActivity extends Activity { private final static String YOUR_SERVER_URL_NO_END_SLASH = "http://192.168.1.104:8888"; private CheckedTextView requestView; private CheckedTextView responseView; private String lastCreatedLocation; public SkeletonActivity() { } /** Called with the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { // DISABLE policy not allowing network connections from main application thread // these calls normally HAVE to be done from worker threads/asynchronous threads/services .. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); super.onCreate(savedInstanceState); // Inflate our UI from its XML layout description. setContentView(R.layout.skeleton_activity); requestView = (CheckedTextView) findViewById(R.id.checkedTextRequest); responseView = (CheckedTextView) findViewById(R.id.checkedTextResponse); // Hook up button presses to the appropriate event handler. ((Button) findViewById(R.id.create)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { PostalAddress address = new PostalAddress(); address.setCity("new city"); address.setCountry("new country"); address.setStreet("new street"); address.setZipCode("zipcode"); // request HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setContentType(new MediaType("application", "json")); HttpEntity<PostalAddress> requestEntity = new HttpEntity<PostalAddress>(address, requestHeaders); // endpoint final String url = YOUR_SERVER_URL_NO_END_SLASH + "/address/"; new AsyncPOST<PostalAddress>(requestEntity) { protected void onPostExecute(Response<String> result) { requestView.setText(url + "\n\n" + request.getHeaders().toString() + "\n\n" + request.getBody().toJSONString()); responseView.setText(result.getResponseEntity().getStatusCode() + "\n\n" + result.getResponseEntity().getHeaders().toString()); lastCreatedLocation = result.getResponseEntity().getHeaders().getLocation().getPath(); }; }.execute(url); } }); // Hook up button presses to the appropriate event handler. ((Button) findViewById(R.id.list)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { RestTemplate restTemplate = new RestTemplate(); // request HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json"))); HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestHeaders); // endpoint final String url = YOUR_SERVER_URL_NO_END_SLASH + "/address/"; new AsyncGET<PostalAddress[]>(requestEntity, PostalAddress[].class) { protected void onPostExecute(Response<PostalAddress[]> result) { requestView.setText(url + "\n\n" + request.getHeaders()); if (result.getEx() != null) { showError(result.getEx()); } else { StringBuilder text = new StringBuilder(); for (PostalAddress address : result.getResponseEntity().getBody()) { text.append(address.toJSONString() + "\n\n"); } //+ requestEntity.getBody().toJSONString()); responseView.setText(result.getResponseEntity().getStatusCode() + "\n\n" + result.getResponseEntity().getHeaders().toString() + "\n\n" + text); } }; }.execute(url); } }); ((Button) findViewById(R.id.update)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { PostalAddress address = new PostalAddress(); address.setCity("updated city"); address.setCountry("new country"); address.setStreet("new street"); address.setZipCode("zipcode"); // request HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setContentType(new MediaType("application", "json")); HttpEntity<PostalAddress> requestEntity = new HttpEntity<PostalAddress>(address, requestHeaders); // endpoint final String url = YOUR_SERVER_URL_NO_END_SLASH + lastCreatedLocation; new AsyncPUT<PostalAddress>(requestEntity) { protected void onPostExecute(Response<Object> result) { requestView.setText(url + "\n\n" + request.getHeaders().toString() + "\n\n" + request.getBody().toJSONString()); if (result.getEx() != null) { showError(result.getEx()); } else { responseView.setText("PUT method in Spring does not support return values.."); } }; }.execute(url); } }); ((Button) findViewById(R.id.delete)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // endpoint final String url = YOUR_SERVER_URL_NO_END_SLASH + lastCreatedLocation; // get reponse new AsyncDELETE() { protected void onPostExecute(Response<Object> result) { requestView.setText(url); if (result.getEx() != null) { showError(result.getEx()); } else { responseView.setText("PUT method in Spring does not support return values.."); } }; }.execute(url); } }); ((Button) findViewById(R.id.query)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { HashMap<String, String> query = new HashMap<String, String>(); query.put("setFilter", "street == streetParam"); query.put("declareParameters", "String streetParam"); query.put("setOrdering", "street desc"); HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("streetParam", "new street"); RESTQuery q = new RESTQuery(); q.setQuery(query); q.setParameters(parameters); // request HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setContentType(new MediaType("application", "json")); HttpEntity<RESTQuery> requestEntity = new HttpEntity<RESTQuery>(q, requestHeaders); // endpoint final String url = YOUR_SERVER_URL_NO_END_SLASH + "/address/q"; // get reponse new AsyncPOST_QUERY<RESTQuery, PostalAddress[]>(requestEntity, PostalAddress[].class) { @Override protected void onPostExecute(Response<PostalAddress[]> result) { requestView.setText(url + "\n\n" + request.getHeaders()); if (result.getEx() != null) { showError(result.getEx()); } else { StringBuilder text = new StringBuilder(); for (PostalAddress address : result.getResponseEntity().getBody()) { text.append(address.toJSONString() + "\n\n"); } responseView.setText(result.getResponseEntity().getStatusCode() + "\n\n" + result.getResponseEntity().getHeaders().toString() + "\n\n" + text); } } }.execute(url); } }); } private void showError(Exception ex) { responseView.setText("Error \n" + ex.toString()); } }