Felipe Souza
Felipe Souza

Reputation: 67

httpUrlconnection returning 500 error code to any call

my android code to call php webservice (json) on Amazon Elastic Beanstalk returning error code 500, but getErrorStream return correct return of my service.

When i call getInputStream i receive error IOException, but URL is correct and when i call with browser return is correct.

my code:

public class AsyncLogin extends AsyncTask<String, String, String> {

    private Activity activity;

    public AsyncLogin(Activity activity)
    {
        this.activity = activity;
    }

    @Override
    protected String doInBackground(String... url) {

        StringBuilder resultado = new StringBuilder();
        try {
            String urlx = url[0];
            URL urlNet = new URL(urlx);
            HttpURLConnection conn= (HttpURLConnection) urlNet.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setUseCaches(false);

            conn.connect();

            int resposta = conn.getResponseCode();

            InputStream retorno = conn.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(retorno));


            String linha = "";
            while ((linha=br.readLine())!= null)
            {
                resultado.append(linha);
            }

            conn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultado.toString();
    }

    @Override
    protected void onPostExecute(String resultado){
        if (Boolean.valueOf(resultado))
        {
            Toast.makeText(activity, "1", Toast.LENGTH_LONG).show();
        }else {
            Toast.makeText(activity, "2", Toast.LENGTH_LONG).show();
        }

    }
}

and my php code is:

<?php
    header('Content-type: application/json');
    $data = array(
    'Teste' => 'Alo mundo!'  
    );

    $resultado = json_encode($data);

    echo $resultado;
?>

Upvotes: 1

Views: 1519

Answers (1)

Felipe Souza
Felipe Souza

Reputation: 67

My PHP have a error on last code line, when i see on webbrowser phpcode print my json without display error, but last line generate error code 500 and when i call with Android the correct error code is 500.

I solve last line (close connection) with my php and this code works fine.

Upvotes: 1

Related Questions