Quando clico no item para enviar os dados o Toast vem vazio. Já verifiquei se minha lista de alunos é convertida em Json e ela é sim. Percebi que uma exceção é lançado, será que é por conta disso? Segue o printstacktrace:
W/System.err: java.io.FileNotFoundException: https://www.caelum.com.br/mobile
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
W/System.err: at br.com.atech.agenda.WebClient.post(WebClient.java:32)
W/System.err: at br.com.atech.agenda.SendStudentsTask.doInBackground(SendStudentsTask.java:33)
W/System.err: at br.com.atech.agenda.SendStudentsTask.doInBackground(SendStudentsTask.java:17)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/System.err: at java.lang.Thread.run(Thread.java:764)
Meu WebClient.class:
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("Accept", "application/json");
connection.setRequestProperty("Content-type", "application/json");
connection.setDoInput(true);
connection.setDoOutput(true); // aqui estamos fazendo um post, por isso tem OutPut
PrintStream output = new PrintStream(connection.getOutputStream());
output.println(json);
connection.connect();
String response = new Scanner(connection.getInputStream()).next();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Minha classe Task:
public class SendStudentsTask extends AsyncTask<Object, Object, String> {
private Context context;
public SendStudentsTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(Object... objects) { //varials?
StudentDAO dao = new StudentDAO(context);
List<Student> students = dao.buscaAlunos();
dao.close();
StudentConverter converter = new StudentConverter();
String json = converter.convertToJson(students);
WebClient client = new WebClient();
String response = client.post(json);
return response;
}
@Override
protected void onPostExecute(String response) {
Toast.makeText(context, response, Toast.LENGTH_LONG).show();
}
}