Created
December 8, 2016 22:45
-
-
Save eichgi/5023acdafbdf9e7703ed11a4b1e20edc to your computer and use it in GitHub Desktop.
Uso de AsyncTask con OkHttp by Square
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.curiousapps.comunidad.activities; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.Color; | |
import android.os.AsyncTask; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ImageView; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.curiousapps.comunidad.R; | |
import com.squareup.picasso.Picasso; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import okhttp3.FormBody; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.RequestBody; | |
import okhttp3.Response; | |
public class AdminActivity extends AppCompatActivity { | |
ListView listaUsuarios; | |
ArrayList<Usuarios> usuarios = new ArrayList<>(); | |
String urlBase = "http://prudominio.esy.es/comunidaditcm/"; | |
AdaptadorUsuarios adaptador; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_admin); | |
listaUsuarios = (ListView) findViewById(R.id.listUsuarios); | |
new UserFeed().execute("lista-usuarios"); | |
} | |
public class AdaptadorUsuarios extends ArrayAdapter<Usuarios> { | |
public AdaptadorUsuarios(Context context, ArrayList<Usuarios> usuarios) { | |
super(context, R.layout.vista_usuario, usuarios); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
LayoutInflater inflater = LayoutInflater.from(getContext()); | |
View item = inflater.inflate(R.layout.vista_usuario, null); | |
if (position % 2 == 0) { | |
item.setBackgroundColor(Color.parseColor("#bd9797")); | |
} | |
TextView tvNombre_usuario = (TextView) item.findViewById(R.id.usuario_nombre); | |
tvNombre_usuario.setText(usuarios.get(position).getNombre()); | |
TextView tvCarrera_usuario = (TextView) item.findViewById(R.id.usuario_carrera); | |
tvCarrera_usuario.setText(usuarios.get(position).getCarrera()); | |
TextView tvEmail_usuario = (TextView) item.findViewById(R.id.usuario_email); | |
tvEmail_usuario.setText(usuarios.get(position).getEmail()); | |
ImageView foto_perfil = (ImageView) item.findViewById(R.id.foto_perfil); | |
String urlRoute = usuarios.get(position).getFoto_perfil(); | |
if (urlRoute != "") { | |
//Aqui va Picasso para pasar imagenes al ImageView | |
Picasso | |
.with(getApplicationContext()) | |
.load(urlBase + urlRoute) | |
.into(foto_perfil); | |
} | |
Toast.makeText(getApplicationContext(), "FOTO", Toast.LENGTH_SHORT).show(); | |
return item; | |
} | |
} | |
public class UserFeed extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... params) { | |
String opcion = params[0]; | |
String url = "http://prudominio.esy.es/comunidaditcm/php/api_servicios.php"; | |
try { | |
OkHttpClient cliente = new OkHttpClient(); | |
RequestBody postData = new FormBody.Builder() | |
.add("servicio", opcion) | |
.build(); | |
Request request = new Request.Builder() | |
.url(url) | |
.post(postData) | |
.build(); | |
Response response = cliente.newCall(request).execute(); | |
String respuesta = response.body().string(); | |
Log.d(getApplicationContext().toString(), "DATOS: " + respuesta); | |
return respuesta; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
@Override | |
protected void onPostExecute(String s) { | |
super.onPostExecute(s); | |
try { | |
JSONArray jsonArray = new JSONArray(s); | |
for (int i = 0; i < jsonArray.length(); i++) { | |
JSONObject json = jsonArray.getJSONObject(i); | |
String nombre = json.getString("nombre"); | |
String email = json.getString("email"); | |
String carrera = json.getString("carrera_usuario"); | |
String url_imagen = json.getString("ruta_imagen"); | |
usuarios.add(new Usuarios(nombre, carrera, email, url_imagen)); | |
Log.d(getApplicationContext().toString(), json.toString()); | |
} | |
adaptador = new AdaptadorUsuarios(getBaseContext(), usuarios); | |
listaUsuarios.setAdapter(adaptador); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public class Usuarios { | |
String nombre, carrera, email, foto_perfil; | |
public Usuarios(String nombre, String carrera, String email, String foto_perfil) { | |
this.nombre = nombre; | |
this.carrera = carrera; | |
this.email = email; | |
this.foto_perfil = foto_perfil; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public String getCarrera() { | |
return carrera; | |
} | |
public String getFoto_perfil() { | |
return foto_perfil; | |
} | |
public String getEmail() { | |
return email; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment