Boa tarde,
Tenho um webservice em que uma das funções é receber uma string que contém um comando SQL, executa-o e gera a resposta em XML. O que eu pretendo é testar essa função e apresentar o XML na minha APP. O meu MainActivity.java é o seguinte:
package com.mycompany.ib4request; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapPrimitive; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; public class MainActivity extends Activity { private final String NAMESPACE = "http:/blablabla.net"; private final String URL = "http://111.111.1.11:0000/WEbService.asmx"; private final String SOAP_ACTION = "http://balblabla.net/Executa_SQL"; private final String METHOD_NAME = "Executa_SQL"; private String TAG = "here"; private static String comando; private static String resposta; Button b; TextView tv; EditText et; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.editText1); tv = (TextView) findViewById(R.id.tv_result); b = (Button) findViewById(R.id.button1); b.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (et.getText().length() != 0 && et.getText().toString() != "") { comando = et.getText().toString(); AsyncCallWS task = new AsyncCallWS(); task.execute(); } else { tv.setText("Please enter SQL command"); } } }); } private class AsyncCallWS extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { Log.i(TAG, "doInBackground"); String resp= getSQL(comando); return resp; } @Override protected void onPostExecute(String result) { Log.i(TAG, "onPostExecute"); tv.setText(result); } @Override protected void onPreExecute() { Log.i(TAG, "onPreExecute"); tv.setText("A Processar..."); } @Override protected void onProgressUpdate(Void... values) { Log.i(TAG, "onProgressUpdate"); } } private String getSQL(String comando) { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //PropertyInfo comandoPI = new PropertyInfo(); //comandoPI.setName("sql"); //comandoPI.setValue(comando); //comandoPI.setType(String.class); request.addProperty("sql",comando); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; //envelope.implicitTypes = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); //androidHttpTransport.debug = true; try { androidHttpTransport.call(SOAP_ACTION, envelope); SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); resposta = response.toString(); // androidHttpTransport.responseDump; } catch (Exception e) { e.printStackTrace(); } /* Log.d("DEBUG",resposta); Log.d("DEBUG- request dump", androidHttpTransport.requestDump); Log.d("DEBUG- response dump",androidHttpTransport.responseDump); Log.d("DEBUG- ",""); */ return resposta; }
A função do webservice apresenta-se desta forma
REQUEST POST /WEbService.asmx HTTP/1.1 Host: 111.111.1.11 Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://blablabla.net/Executa_SQL" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Executa_SQL xmlns="http://blablabla.net/"> <sql>string</sql> </Executa_SQL> </soap:Body> </soap:Envelope> RESPONSE HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Executa_SQLResponse xmlns="http://blablabla.net/"> <Executa_SQLResult>xmlxml</Executa_SQLResult> </Executa_SQLResponse> </soap:Body> </soap:Envelope>
E finalmente o log do Debugger quando utilizo esta linha de código
request.addProperty("sql",comando);
é o seguinte:
06-05 11:05:19.359 1633-1662/com.mycompany.ib4request W/System.err﹕ java.net.SocketTimeoutException 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.android.okio.Okio$2.read(Okio.java:113) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.android.okio.RealBufferedSource.indexOf(RealBufferedSource.java:147) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:94) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:175) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:616) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:491) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.transport.ServiceConnectionSE.getResponseCode(ServiceConnectionSE.java:103) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:192) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.mycompany.ib4request.MainActivity.getSQL(MainActivity.java:112) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.mycompany.ib4request.MainActivity.access$300(MainActivity.java:18) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.mycompany.ib4request.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:65) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at com.mycompany.ib4request.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:61) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 06-05 11:05:19.399 1633-1662/com.mycompany.ib4request W/System.err﹕ at java.lang.Thread.run(Thread.java:818) 06-05 11:05:19.403 1633-1633/com.mycompany.ib4request I/here﹕ onPostExecute
Quando em vez dessa linha, uso:
PropertyInfo comandoPI = new PropertyInfo();comandoPI.setName("sql"); comandoPI.setValue(comando); comandoPI.setType(String.class); request.addProperty(comandoPI);
Gera-me um log diferente, com um erro em Português, suponho que o request esteja a ser feito, mas de forma errada portanto.
06-05 11:53:04.982 1902-1902/com.mycompany.ib4request I/here﹕ onPreExecute06-05 11:53:04.986 1902-1934/com.mycompany.ib4request I/here﹕ doInBackground 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ SoapFault - faultcode: 'soap:Server' faultstring: 'O servidor não conseguiu processar o pedido. ---> A referência de objecto não foi definida como uma instância de um objecto.' faultactor: 'null' detail: org.kxml2.kdom.Node@3f3213c8 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:137) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.transport.Transport.parseResponse(Transport.java:129) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.parseResponse(HttpTransportSE.java:301) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:274) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at com.mycompany.ib4request.MainActivity.getSQL(MainActivity.java:113) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at com.mycompany.ib4request.MainActivity.access$300(MainActivity.java:19) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at com.mycompany.ib4request.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:66) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at com.mycompany.ib4request.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:62) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 06-05 11:53:05.190 1902-1934/com.mycompany.ib4request W/System.err﹕ at java.lang.Thread.run(Thread.java:818) 06-05 11:53:05.190 1902-1902/com.mycompany.ib4request I/here﹕ onPostExecute
Informo ainda que testei o request SELECT SUM (1) num software à parte e o webservice está a gerar o XML correspondente, portanto suponho que o problema esteja no meu código. Agradeço qualquer ajuda.
Cumps