Oi Pessoal,
Meu código está dando um erro quando tentar fazer a conexão com o servidor que eu ainda não consegui identificar.
A classe WebClient está assim:
public class WebClient {
private URL url;
public WebClient(String url) throws MalformedURLException{
this.url = new URL(url);
}
@SuppressWarnings("resource")
public String post (String json) throws InterruptedException, IOException{
String resposta = null;
HttpURLConnection connection;
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();
resposta = new Scanner(connection.getInputStream()).next();
return resposta;
}
}
O método doInBackground:
protected String doInBackground(Object... params) {
DBHelper dbHelper = new DBHelper(this.ctx);
List<Aluno> alunos = dbHelper.getListaAlunos();
String json = new AlunoConverter().toJSON(alunos);
try {
WebClient cliente = new WebClient("http://www.caelum.com.br/mobile");
String resp;
resp = cliente.post(json);
return resp;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
E o LogCat:
10-08 13:32:26.138: E/AndroidRuntime(1260): FATAL EXCEPTION: AsyncTask #1
10-08 13:32:26.138: E/AndroidRuntime(1260): Process: br.com.lacouth.cadastro, PID: 1260
10-08 13:32:26.138: E/AndroidRuntime(1260): java.lang.RuntimeException: An error occured while executing doInBackground()
10-08 13:32:26.138: E/AndroidRuntime(1260): at android.os.AsyncTask$3.done(AsyncTask.java:300)
10-08 13:32:26.138: E/AndroidRuntime(1260): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
10-08 13:32:26.138: E/AndroidRuntime(1260): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
10-08 13:32:26.138: E/AndroidRuntime(1260): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
10-08 13:32:26.138: E/AndroidRuntime(1260): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-08 13:32:26.138: E/AndroidRuntime(1260): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-08 13:32:26.138: E/AndroidRuntime(1260): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-08 13:32:26.138: E/AndroidRuntime(1260): at java.lang.Thread.run(Thread.java:841)
10-08 13:32:26.138: E/AndroidRuntime(1260): Caused by: java.util.NoSuchElementException
10-08 13:32:26.138: E/AndroidRuntime(1260): at java.util.Scanner.next(Scanner.java:961)
10-08 13:32:26.138: E/AndroidRuntime(1260): at java.util.Scanner.next(Scanner.java:934)
10-08 13:32:26.138: E/AndroidRuntime(1260): at br.com.lacouth.suporte.WebClient.post(WebClient.java:46)
10-08 13:32:26.138: E/AndroidRuntime(1260): at br.com.lacouh.task.EnvioDeAlunos.doInBackground(EnvioDeAlunos.java:39)
10-08 13:32:26.138: E/AndroidRuntime(1260): at br.com.lacouh.task.EnvioDeAlunos.doInBackground(EnvioDeAlunos.java:1)
10-08 13:32:26.138: E/AndroidRuntime(1260): at android.os.AsyncTask$2.call(AsyncTask.java:288)
10-08 13:32:26.138: E/AndroidRuntime(1260): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-08 13:32:26.138: E/AndroidRuntime(1260): ... 4 more