Olá. Já verifiquei o código e está tudo correto, mas ao finalizar o status de envio, barra de progressão finalizada, o app finaliza e fecha a aplicação.
06-02 21:11:59.331 20302-20902/br.com.jacquesalexandre.agenda1 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: br.com.jacquesalexandre.agenda1, PID: 20302
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.util.NoSuchElementException
public class EnviaAlunosTask extends AsyncTask <Void, Void, String> {
private Context context;
private ProgressDialog dialog;
public EnviaAlunosTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(context, "Aguarde", "Enviado 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();
}
}
...public class ListaAlunosActivity... @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_lista_alunos, menu); return true; }
public class WebClient {
public String post(String json){
try {
URL url = new URL("http://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;
}
}
public class AlunoConverter {
public String converteparaJSON(List<Aluno> alunos) {
JSONStringer js = new JSONStringer();
try {
js.object().key("List").array().object().key("aluno").array();
for(Aluno aluno : alunos){
js.object();
js.key("nome").value(aluno.getNome());
js.key("nota").value(aluno.getNota());
js.endObject();
}
js.endArray().endObject().endArray().endObject();
} catch (JSONException e) {
e.printStackTrace();
}
return js.toString();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_enviar_notas:
new EnviaAlunosTask(this).execute();
break;
}
return super.onOptionsItemSelected(item);
}