Advertisement
LoonerSF

Tema Client

Aug 13th, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. package com.mx.ipn.escom.clientRest.client;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.client.HttpClient;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. import org.apache.http.util.EntityUtils;
  12. import org.json.JSONArray;
  13. import org.json.JSONObject;
  14.  
  15. import android.util.Log;
  16.  
  17. import com.mx.ipn.escom.clientRest.modelo.Tema;
  18.  
  19.  
  20. public class TemaClient{
  21.  
  22.     public List<Tema> getTemas(){
  23.        
  24.         HttpClient httpClient = new DefaultHttpClient();
  25.        
  26.         HttpGet getData = new HttpGet("http://192.168.1.64:8080/TesterRest/rest/temas");
  27.         getData.setHeader("content-type", "application/json");
  28.         List<Tema> temas = new ArrayList<Tema>();
  29.         try{
  30.             HttpResponse resp= httpClient.execute(getData);
  31.             String respStr = EntityUtils.toString(resp.getEntity());
  32.             JSONArray data = new JSONArray(respStr);//I have the error here
  33.             for(int i=0; i<data.length();i++){
  34.                 JSONObject obj = data.getJSONObject(i);
  35.                 Integer id = obj.getInt("idTema");
  36.                 String nbName = obj.getString("nbTema");
  37.                 String dsName = obj.getString("dsTema");
  38.                 Tema tema = new Tema(id, nbName, dsName);
  39.                 temas.add(tema);               
  40.             }
  41.         }catch (Exception e) {
  42.             // TODO: handle exception
  43.             Log.e("Servicio Rest", "Error!", e);
  44.         }
  45.         for(Tema tema : temas)
  46.             System.out.println(tema.getIdTema() + " " + tema.getNbTema() + " " + tema.getDsTema());
  47.         return temas;
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement