Uso KSOAP2 library en Android Studio, SQL SERVER con una tabla llamada "Clientes" y el web service ASP.NET en Visual Studio 2017.
Cuando llamo "TareaWSConsulta" y "TareaWSEnviar" capturo las siguientes excepciones:
11-05 07:14:56.592 12155-12248/com.example.ruper.serviciosweb_soap_rest E/CONSULTA: java.net.SocketException: Permission denied
11-05 07:15:10.878 12155-12172/com.example.ruper.serviciosweb_soap_rest E/ENVIAR: java.net.SocketException: Permission denied
No se cual debe ser el problema. Visual Studio esta abierto y con el WebService en marcha. Y a la app de Android le doy el permiso de INTERNET... Alguna idea de porque me dan estos errores??? Gracias de antemano!
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ruper.serviciosweb_soap_rest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-permission android:name="android.permission.INTERNET"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity class:
package com.example.ruper.serviciosweb_soap_rest;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends AppCompatActivity {
EditText etxtNombre, etxtTelefono;
Button btnEnviar, btnConsultar;
ListView lstClientes;
TextView tviewResultado;
String NAMESPACE="ruperdevcoder";
String URL="http://10.0.2.2:51629/ServicioClientes.asmx";
String METHOD_NAME="NuevoClienteSimple";
String SOAP_ACTION="ruperdevcoder/NuevoClienteSimple";
private static final int MY_PERMISSIONS_REQUEST_INTERNET=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etxtNombre = (EditText)findViewById(R.id.etxtNombre);
etxtTelefono = (EditText)findViewById(R.id.etxtTelefono);
btnEnviar = (Button)findViewById(R.id.btnEnviar);
btnConsultar = (Button)findViewById(R.id.btnConsultar);
tviewResultado = (TextView)findViewById(R.id.tviewResultado);
lstClientes = (ListView)findViewById(R.id.lstClientes);
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET);
if(permissionCheck!= PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.INTERNET)){
}else{
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.INTERNET
},MY_PERMISSIONS_REQUEST_INTERNET);
}
}
btnConsultar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TareaWSConsulta tarea = new TareaWSConsulta();
tarea.execute();
}
});
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TareaWSEnviar tarea = new TareaWSEnviar();
tarea.execute(etxtNombre.getText().toString(),etxtTelefono.getText().toString());
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[],int[]grantResults){
switch(requestCode){
case MY_PERMISSIONS_REQUEST_INTERNET:{
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
}else{
}
return;
}
}
}
private class TareaWSConsulta extends AsyncTask<String,Integer,Boolean> {
private Cliente[] listaClientes;
@Override
protected Boolean doInBackground(String... params) {
Boolean result=true;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transporte = new HttpTransportSE(URL);
try{
transporte.call(SOAP_ACTION, envelope);
SoapObject resSoap=(SoapObject)envelope.getResponse();
listaClientes = new Cliente[resSoap.getPropertyCount()];
for(int i=0; i<listaClientes.length;i++){
SoapObject ic = (SoapObject) resSoap.getProperty(i);
Cliente cli = new Cliente();
cli.id=Integer.parseInt(ic.getProperty(0).toString());
cli.nombre=ic.getProperty(1).toString();
cli.telefono=Integer.parseInt(ic.getProperty(2).toString());
listaClientes[i]=cli;
}
}catch(Exception e) {
Log.e("CONSULTA", e.toString());
result = false;
}
return result;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean result) {
if(result){
final String[] datos = new String[listaClientes.length];
for(int i=0;i<listaClientes.length;i++){
datos[i]=listaClientes[i].nombre;
ArrayAdapter<String> adaptador = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, datos);
lstClientes.setAdapter(adaptador);
}
}else{
tviewResultado.setText("Error!");
}
}
}
private class TareaWSEnviar extends AsyncTask<String,Integer,Boolean>{
public TareaWSEnviar() {
super();
}
@Override
protected Boolean doInBackground(String... params) {
boolean result=true;
String nombre= params[0];
String telefono= params[1];
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("nombre", nombre);
request.addProperty("telefono", telefono);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE transporte = new HttpTransportSE(URL);
try{
transporte.call(SOAP_ACTION, envelope);
SoapPrimitive resultado_xml= (SoapPrimitive)envelope.getResponse();
String res=resultado_xml.toString();
if(!res.equals(1)){
result=false;
}
}catch(Exception e){
Log.e("ENVIAR",e.toString());
result = false;
}
return result;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean result) {
if(result){
tviewResultado.setText("INSERTADO OK!");
}else{
tviewResultado.setText("ERROR!");
}
}
}
}