1
I am trying to make my list view popular with a json, it check the information of another json and return me a link in which I will use to fill photos but everything goes right with you setar
link in a class but when it comes time to receive it always get null
GET.class
<- class where I get the 1st json for popular
class GET extends AsyncTask<String,String,String> {
Activity activity;
String nome;
String nome2;
String idpaciente;
String idmedico;
ListView lv;
public GET(Activity activity, ListView lv, String nome) {
this.activity = activity;
this.lv = lv;
this.nome = nome;
}
@Override
protected String doInBackground(String... strings) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String resposta = null;
try {
if(Utils.verifyConnection(activity)) {
URL url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
resposta = buffer.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return resposta;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
JSONArray array = null;
try {
if(Utils.verifyConnection(activity)) {
array = new JSONArray(s);
int i = 0;
List<Consulta> consultas = new ArrayList<>();
String dados = "";
while (i < array.length()) {
JSONObject obj = array.getJSONObject(i);
int id = obj.getInt("id");
String medico = obj.getString("medico");
String paciente = obj.getString("paciente");
int status = obj.getInt("status");
String rece = obj.getString("receita");
String horario = obj.getString("horario");
idmedico = obj.getString("idmedico");
idpaciente = obj.getString("idpaciente");
String espec = obj.getString("especialidade");
String remedios = obj.getString("remediosMinistrados");
String observacoes = obj.getString("observacoes");
String tratamento = obj.getString("tratamento");
String imagem = obj.getString("imagem");
String linkreceita = obj.getString("linkReceita");
new PHOTO(activity, lv, nome).execute("http://infasstec.com.br/desenvolvimento/android/foto.json");
//Toast.makeText(activity, "id"+nome +"idpaci"+ idpaciente +"idmed" + idmedico, Toast.LENGTH_LONG).show();
//Toast.makeText(activity, id+ medico+ idmedico+paciente+idpaciente, Toast.LENGTH_SHORT).show();
if (nome.equals(idmedico)) {
if (status == 1) {
String teste = idpaciente;
guardaFoto guarda = new guardaFoto();
new fotolist(activity, lv, teste).execute("http://infasstec.com.br/desenvolvimento/android/foto.json"); <- chamo a outra class para receber o link da foto
Consulta consulta = new Consulta();
consulta.setId(id);
consulta.setMed(medico);
consulta.setPaci(paciente);
consulta.setAbertas(status);
consulta.setRec(rece);
consulta.setHorario(data_certa(horario));
consulta.setEspe(espec);
consulta.setReme(remedios);
consulta.setDesc(observacoes);
consulta.setTrat(tratamento);
consulta.setImagem(imagem);
consulta.setLink(linkreceita);
//consulta.setFoto(guarda.getPicture());
consulta.setNome("Paci. " + paciente);
String a = guarda.getPicture();
//Toast.makeText(activity, i +a, Toast.LENGTH_SHORT).show();
consultas.add(consulta);
} else {
}
} else if (nome.equals(idpaciente)) {
if (status == 1) {
String teste = idmedico;
new fotolist(activity, lv, teste).execute("http://infasstec.com.br/desenvolvimento/android/foto.json"); <- chamo a outra class para receber o link da foto
guardaFoto guarda = new guardaFoto();
Consulta consulta = new Consulta();
consulta.setId(id);
consulta.setMed(medico);
consulta.setPaci(paciente);
consulta.setAbertas(status);
consulta.setRec(rece);
consulta.setHorario(data_certa(horario));
consulta.setEspe(espec);
consulta.setReme(remedios);
consulta.setDesc(observacoes);
consulta.setTrat(tratamento);
consulta.setImagem(imagem);
consulta.setLink(linkreceita);
consulta.setFoto(guarda.getPicture()); <- Pego o link que foi setado no getPicture()
consulta.setNome("Dr. " + medico);
//Toast.makeText(activity, consulta.getFoto(), Toast.LENGTH_SHORT).show();
consultas.add(consulta);
} else {
}
}
i++;
}
lv.setAdapter(new ConsultasAdapter(activity, consultas));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public String data_certa(String text)
{
String res = "";
try
{
String partes[] = text.split(" ");
String partes_partes[] = partes[0].split("-");
res += partes_partes[2] + "-" + partes_partes[1] + "-" + partes_partes[0] + " " + partes[1];
}
catch (Exception e)
{
res = text;
}
return res;
}
}
fotolist.class
<- Class where I take 2 json to compare and set the links
class fotolist extends AsyncTask<String,String,String> {
Activity activity;
String nome;
String nome2;
String idpaciente;
String idmedico;
static String link;
ListView lv;
public fotolist(Activity activity, ListView lv, String teste) {
this.activity = activity;
this.lv = lv;
this.nome = teste;
}
@Override
protected String doInBackground(String... strings) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String resposta = null;
try {
if(Utils.verifyConnection(activity)) {
URL url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
resposta = buffer.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return resposta;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
JSONArray array = null;
try {
if(Utils.verifyConnection(activity)) {
array = new JSONArray(s);
int i = 0;
List<Consulta> consultas = new ArrayList<>();
String dados = "";
while (i < array.length()) {
JSONObject obj = array.getJSONObject(i);
String id = obj.getString("id");
//String nome1 = obj.getString("nome");
String foto = obj.getString("foto");
if (nome.equals(id)) {
guardaFoto guarda = new guardaFoto();
guarda.setPicture(foto); <- seto o link para receber com o getPicture()
link = guarda.getPicture();
//Toast.makeText(activity, link, Toast.LENGTH_SHORT).show();
}
i++;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
guardaFoto.class
<- class where I store and pick up the links
public class guardaFoto {
String foto;
public void setPicture(String foto) {
this.foto = foto;
}
public String getPicture() { return foto; }
}
Using breakpoints I can see that the guardaFoto.class
received the link but when trying to receive always returns null
The part of
while
which arrow the link onsetPictute()
it will buy the id and will always be a link that I will receive so it would not be the case to use a list.– Matheus Rohwedder
However I would have to receive this link by
getPicture()
and everything is being done on onCreate because they are images of users who would stand next to their medical appointments– Matheus Rohwedder
Well, you have two solutions, by a Reader and inform when you pick up a new one (or arrange your onPostExecute), or else Voce passes the Imageview to your Task and already arrow the image after downloading that is easier.
– Marcos Vasconcelos