Skip to content

Instantly share code, notes, and snippets.

@Dayvidhms
Created August 13, 2017 20:42
Show Gist options
  • Save Dayvidhms/0406c7aa3479e7a0f9b5f8c5842b5272 to your computer and use it in GitHub Desktop.
Save Dayvidhms/0406c7aa3479e7a0f9b5f8c5842b5272 to your computer and use it in GitHub Desktop.
package com.meleva.foxdesenvolvimento.meleva.ws;
import android.os.StrictMode;
import android.util.Log;
import com.meleva.foxdesenvolvimento.meleva.controller.Motorista;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.ResponseHandler;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.impl.client.BasicResponseHandler;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
public class MotoristaWS {
private static final String URL_SELECT = "http://192.168.25.172:8080/TCC_Database/webresources/motorista/get";
private static final String URL_INSERT = "http://192.168.25.172:8080/TCC_Database/webresources/motorista/post";
public List<Motorista> getMotorista() {
//Responsavel pelas requisições e chamadas ao webservices
HttpClient httpClient = new DefaultHttpClient();
//Tipo de requisição e para onde é feita
HttpGet getDados = new HttpGet(URL_SELECT);
String retorno = "";
try {
//Tipo de requisição (Sincrona ou Assincrona)
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//Processa respostas vindas do servidor e a transforma em determinado tipo
ResponseHandler<String> responseHandler = new BasicResponseHandler();
//executa a chamada
String respostaServidor = httpClient.execute(getDados, responseHandler);
JSONArray array = new JSONArray(respostaServidor);
List<Motorista> listMotorista = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
Motorista motorista = new Motorista();
motorista.setFoto(o.getString("foto"));
motorista.setNome(o.getString("nome"));
motorista.setMensalidade(o.getDouble("mensalidade"));
motorista.setTelefone(o.getString("telefone"));
listMotorista.add(motorista);
}
return listMotorista;
} catch (Exception ex) {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment