-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,
<?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);
}
}
}
@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
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.
– Matheus Martins