Solucionado (ver solução)
Solucionado
(ver solução)
7
respostas

Quando tento enviar os alunos para webservice https://www.caelum.com.br/mobile recebo um ero java.io.FileNotFoundException:

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();
    }
}
7 respostas

O servidor só aceita requisições do tipo POST. Então é necessário adicionar essa linha à sua connection:

connection.setRequestMethod("POST");

Felipe, obrigado pelo contato. Eu coloquei a sua sugestão mais não funcionou.

Deixa eu ver seu código com a modificação sugerida, por favor.

connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST");

solução!

Adicione essa linha também:

connection.setRequestProperty("Content-type","application/json");

Valeu Felipe, funcionou!

Obrigado pela atenção.

Boa! Qualquer dúvida é só abrir outro tópico e a gnt te ajuda! ;)

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software