o arquivo json que é gerado: ALUNOSJSON: {"list":[{"aluno":[{"nome":"Paulo Henrique Amorin","nota":7},{"nome":"Fernando Brito","nota":8},{"nome":"Miguel do Rosario","nota":8}]}]}
O código da resposta http: Código de resposta : 500
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-tye","application/json");
connection.setDoInput(true);
connection.setDoOutput(true);
PrintStream output = new PrintStream(connection.getOutputStream());
output.println(json);
connection.connect();
int responseCode = connection.getResponseCode();
Log.i("WEBSERVICE", "Código de resposta : " + responseCode);
Scanner scanner = new Scanner(connection.getInputStream());
StringBuilder sb = new StringBuilder();
while (scanner.hasNext()){
sb.append(scanner.nextLine());
}
String resposta = sb.toString();
Log.d("json",resposta);
return resposta;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
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","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);
Log.i("ALUNOSJSON", json);
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();
}
}