Galera,Já consegui implementar meu WebService rest.Pra consumir no android usei a biblioteca volley, seguindo esse tutorial.O exemplo do tutorial funcionou tanto num AVD para a api 8 quanto para a API 19.Para usar com o meu WS fiz da seguinte forma:public class MainActivity extends Activity implements
Response.Listener<JSONObject>, Response.ErrorListener {// JSON Node namesprivate static final String TAG_VEICULO = "veiculo";private static final String TAG_ANO = "ano";private static final String TAG_MARCA = "marca";private static final String TAG_MODELO = "modelo";private static final String TAG_FOTOS = "fotos";private static final String TAG_ENDERECO = "endereco";private static final String TAG_DESCRICAO = "descricao";private static final String TAG_TITULO = "titulo";private static final String TAG_TIPO = "tipo";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String url = "http://10.0.2.2:8080/BlogWS_REST/main/getSampleVehicleList";RequestQueue queue = Volley.newRequestQueue(this);JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, // Requisição via HTTP_GETurl, // url da requisiçãonull, // JSONObject a ser enviado via POSTthis, // Response.Listenerthis); // Response.ErrorListenerint socketTimeout = 30000;// 30 seconds - change to what you wantRetryPolicy policy = new DefaultRetryPolicy(socketTimeout,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);jsObjRequest.setRetryPolicy(policy);queue.add(jsObjRequest);}@Overridepublic void onResponse(JSONObject response) {Toast.makeText(this, "response", Toast.LENGTH_SHORT).show();ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>();try {// Getting JSON Array nodeJSONArray vehiclesJSON = response.getJSONArray(TAG_VEICULO);// looping through All Vehiclesfor (int i = 0; i < vehiclesJSON.length(); i++) {Vehicle vehicle = new Vehicle();JSONObject v = vehiclesJSON.getJSONObject(i);vehicle.setMarca(v.getString(TAG_MARCA));vehicle.setModel(v.getString(TAG_MODELO));vehicle.setYear(v.getString(TAG_ANO));ArrayList<Photo> photos = new ArrayList<>();// Getting JSON Array nodeJSONArray photosJSON = v.getJSONArray(TAG_FOTOS);// looping through All Vehiclesfor (int j = 0; j < photosJSON.length(); j++) {Photo photo = new Photo();JSONObject f = photosJSON.getJSONObject(j);photo.setAddress(f.getString(TAG_ENDERECO));photo.setTitle(f.getString(TAG_TITULO));photo.setDescription(f.getString(TAG_DESCRICAO));photo.setType(f.getString(TAG_TIPO));photos.add(photo);}vehicle.setPhotos(photos);vehicles.add(vehicle);}} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onErrorResponse(VolleyError error) {Toast.makeText(this, "Erro!", Toast.LENGTH_SHORT).show();}}Ao debugar no AVD da api 8 funcionou bem.Mas no 19 não funcionou de jeito nenhum. Dá um volley.TimeoutErrorDeram a dica de usar:int socketTimeout = 30000;// 30 seconds - change to what you wantRetryPolicy policy = new DefaultRetryPolicy(socketTimeout,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);jsObjRequest.setRetryPolicy(policy);Mas também não funcionou.Alguém aí que já utilizou o volley pode dar uma luz.--Atenciosamente,Jean Santiago
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.