Flavio bom dia,
O erro persiste ? Fiz alguns testes aqui, e mesmo se o retorno for vazio, o seu Toast seria criado, entretanto não mostrando nada !
Vi ali no erro que ele volta duas linhas dentro da sua AsyncTask, a linha 20 e a 41, você falar quais são esses erros ?
Vou colocar aqui o código junto dos imports dessas minhas classes, tenta fazer o teste ai e vemos o que acontece !
package br.com.caelum.fj57design.servidor;
import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Scanner;
/**
* Created by matheus on 10/09/15.
*/
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.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
PrintStream saida = new PrintStream(connection.getOutputStream());
saida.println(json);
connection.connect();
String resposta = new Scanner(connection.getInputStream()).next();
return resposta;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
E agora :
package br.com.caelum.fj57design.asynctask;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.util.List;
import br.com.caelum.fj57design.Dao.AlunoDao;
import br.com.caelum.fj57design.converter.AlunoConverter;
import br.com.caelum.fj57design.modelo.Aluno;
import br.com.caelum.fj57design.servidor.WebClient;
/**
* Created by matheus on 10/09/15.
*/
public class EnviaDadosServidor extends AsyncTask<String, Void, String> {
private Context context;
private ProgressDialog progressDialog;
public EnviaDadosServidor(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(context, "Aguarde ...", "Enviando dados para o servidor", false, false);
}
@Override
protected void onPostExecute(String media) {
super.onPostExecute(media);
progressDialog.dismiss();
Toast.makeText(context, media, Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
AlunoDao dao = new AlunoDao(context);
List<Aluno> alunos = dao.pegaAlunos();
dao.close();
AlunoConverter converter = new AlunoConverter();
String json = converter.toJson(alunos);
WebClient webClient = new WebClient();
String resposta = webClient.post(json);
return resposta;
}
}