Segui as aulas , porem ao clicar no botão enviar notas ,aparece um Toast pequeno sem nenhum valor. No logcat o json está retornando os valores da seguinte forma "2020-05-18 18:50:25.647 19194-19620/com.example.listaalunos I/Teste json: {"list":[{"aluno":[{"nome":"Robson","nota":10},{"nome":"Douglas","nota":8}]}]}" Segue os códigos para analise.
Poderiam me ajudar ?
package com.example;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import com.example.converter.AlunoConverter;
import com.example.dao.AlunoDAO;
import com.example.modelo.Aluno;
import java.util.List;
public class EnviarAlunosTask extends AsyncTask<Void, Void, String> {
private Context context;
private ProgressDialog dialog;
public EnviarAlunosTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(context, "Aguarde", "Enviando alunos...", true, true);
}
@Override
protected String doInBackground(Void... params) {
AlunoDAO dao = new AlunoDAO(context);
List<Aluno> alunos = dao.buscaAlunos();
dao.close();
AlunoConverter conversor = new AlunoConverter();
String json = conversor.converteParaJSON(alunos);
WebClient client = new WebClient();
String resposta = client.post(json);
return resposta;
}
@Override
protected void onPostExecute(String resposta) {
dialog.dismiss();
Toast.makeText(context, resposta, Toast.LENGTH_LONG).show();
}
}
package com.example;
import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class WebClient {
public String post(String json) {
try {
URL url = new URL("https://www.caelum.com.br/mobile");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
PrintStream output = new PrintStream(connection.getOutputStream());
output.println(json);
connection.connect();
Scanner scanner = new Scanner(connection.getInputStream());
String resposta = scanner.next();
return resposta;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}