Android emulator accesses web service by browser but not by APP

Asked

Viewed 135 times

-1

Good morning, what happens is the following my emulator accesses my web service by browser at http://192.168. 2.192/index.php, it shows the result of the page, inserir a descrição da imagem aqui

<?php
	$minha_info = "TESTE JSON";
   	echo json_encode($minha_info);
?>

as far as I understood it is to work because, on the page has an information, this is my code in android studio

public class MainActivity extends AppCompatActivity {


    private Button button;
    private TextView textView;
    private TextInputEditText cep;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
        cep = findViewById(R.id.cep);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyTask task = new MyTask();
                String numero = cep.getText().toString();
                String urlApi = "http://192.168.2.192/teste.php";
                task.execute(urlApi);

            }
        });

    }
    class MyTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... strings) {
            String stringurl = strings[0];
            InputStream inputStream = null;
            InputStreamReader inputStreamReader = null;
            StringBuffer  buffer = null;
            try {
                URL url = new URL(stringurl);
                HttpURLConnection conexao = (HttpURLConnection) url.openConnection();
                inputStream = conexao.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader reader = new BufferedReader(inputStreamReader);
                buffer = new StringBuffer();
                String linha = "";
                while ((linha = reader.readLine()) != null){
                    buffer.append(linha);
                }


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return buffer.toString();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            textView.setText(s);
        }
    }
}
however, it simply crashes, and when I test with external web services it works, but only with my local web service it doesn’t work.

@EDIT Problem solved by adding networkacess

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

the application was without permission to access the local network

1 answer

0


First you need to add permissions to your Androidmanifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

After this setting, use ip: 10.0.2.2 in order to be able to access your localhost. Documentation: here

Don’t forget to use the http protocol in the call: http://10.0.2.2/recurso

  • 1

    Good morning, I managed to solve, I had not attacked me in debug, it was exactly the lack of ACCESS_NETWORK_STATE, no debug was appearing error that had not permission to access the server, thank you for the force.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.