Connecting to external webservice fail to response on Android - android

I'm trying to connect to a external webservice from an Android app, but currently it crashes on the XML response, please help me notice if the call structure is built incorrectly or any the response code is crashed. Here is my code:
public class sri extends AsyncTask<String, String, String>
{
public final static String URL = "http://qa-suia.ambiente.gob.ec:8092/suiawebservices/SuiaServices?wsdl";
public static final String NAMESPACE = "http://client.ambiente.gob.ec/";
public static final String SOAP_ACTION_PREFIX = "/";
private static final String METHOD = "getRuc";
private String resp;
SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
protected String doInBackground(String... param)
{
SoapObject request = new SoapObject(NAMESPACE,METHOD);
//String JsonString=Serializa();
request.addProperty("user","99999999");
request.addProperty("pass","xxxxxxxxxxxx");
request.addProperty("rucNumber","99999999999");
//sobre.dotNet=true;
sobre.setOutputSoapObject(request);
try
{
HttpTransportSE transporte=new HttpTransportSE(URL);
transporte.call(NAMESPACE+METHOD, sobre);
} catch (Exception e)
{
String msn="error";
return msn;
}
try {
if (sobre != null) {
SoapPrimitive response = (SoapPrimitive)sobre.getResponse();
resp=response.toString();
}
else
{
}
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}
protected void onProgressUpdate(Integer... values)
{
}
public void onPreExecute()
{
}
public void onPostExecute(String result)
{
String resultado=result;
}

Here's working code for what I'm doing calling a .Net Service (It looks like you're not putting your request into an envelope):
private static SoapPrimitive callProcServiceForScalar(String serviceMethod, String storedProc, String params) throws Exception {
String NAMESPACE = "http://" + YourIPAddress + "/";
String METHOD_NAME = getMethodName(serviceMethod);
String SOAP_ACTION = NAMESPACE + METHOD_NAME;
String URL = "http://" + YourIP + "/YourService.asmx";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("sPassword", sYourPassword);
request.addProperty("sData", sServerDB);
request.addProperty("sSP_Name", storedProc);
request.addProperty("sParam", params);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// Enable the below property if consuming .Net service
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
numberOfBytesTransmitted = numberOfBytesTransmitted + StringToBytes(request.toString());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, timeout);
SoapPrimitive returnable = null;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
returnable = (SoapPrimitive)envelope.getResponse();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Msg:" + e.getMessage() + "; SP:" + storedProc + "; Params: " + params + "; Method:" + METHOD_NAME);
}
return returnable;
}

Related

org.xmlpull.v1.XmlPullParserException: expected: START_TAG in api calling

I am new bee in android. I am trying parse data using soap api. Following is my snippet code. When I run the project I am getting following error can any one help me with this? Following is my snippet code. I am not able to find the issue what exactly I am facing. sorry for bad english.
Error
W/System.err: org.xmlpull.v1.XmlPullParserException: expected:
START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope
(position:START_TAG #4:44 in java.io.InputStreamReader#7f5b56a)
Code
public class GetReport extends Activity {
private static final String NAMESPACE = "https://www.myweb.co.ke/Wt/"; // com.service.ServiceImpl
private static final String URL = "https://www.myweb.co.ke/Wt/webtask.asmx";
private static final String METHOD_NAME = "GetProductListing";
private static final String SOAP_ACTION = NAMESPACE+METHOD_NAME;
private String webResponse = "";
private Handler handler = new Handler();
private Thread thread;
private TextView textView1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(1);
setContentView(R.layout.test_demo);
textView1 = (TextView) findViewById(R.id.ttte);
startWebAccess("title");
}
public void startWebAccess(String a) {
final String aa = a;
thread = new Thread() {
public void run() {
try {
Log.d("Req value0R", "Starting...");// log.d is used for
// debug
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
/* request.addProperty("ClientCode", "64396");
request.addProperty("key", "Om$#!##M^#R");*/
request.addProperty("User Name", "1234");
request.addProperty("Password", "4321");
Log.d("Req value1", request.toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(
URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject objectResult = (SoapObject) envelope.bodyIn;
webResponse = objectResult.toString();
System.out.println("response: " + webResponse);
} catch (SoapFault sp) {
sp.getMessage();
System.out.println("error = " + sp.getMessage());
} catch (Exception e) {
System.out.println("problem8");
e.printStackTrace();
webResponse = "Connection/Internet problem";
}
handler.post(createUI);
}
};
thread.start();
}
final Runnable createUI = new Runnable() {
public void run() {
if (webResponse != null) {
textView1.setText(webResponse);
} else {
webResponse = "No data provided presently";
textView1.setText(webResponse);
}
}
};
}

Image loading using picasso library from url

initially, I am fetching image URL from server to a string then using Picasso library I am trying to load the image
I am able to get image URL from the server like thislogcat
but image not loaded in the image view. when tried placing direct URL it works.
public class MainActivity extends AppCompatActivity {
ImageView im;
Button bm;
String str ;
private static String NAMESPACE = "http://telview360/";
private static String URL = "http://54.179.134.139/viView360Service/WebService.asmx?WSDL";
private static String SOAP_ACTION = "http://telview360/ImageDetails";
private static String METHOD_NAME = "ImageDetails";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_image);
final Thread networkThread = new Thread() {
#Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
str = response.toString();
Log.d("Webservice", " response " + str);
} catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
bm = (Button) findViewById(R.id.btn_load_image);
bm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
im = (ImageView) findViewById(R.id.image);
Picasso.with(getApplicationContext()).load(str).into(im);
}
});
}
}
add this to your code
final Thread networkThread = new Thread() {
#Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
str = response.toString();
Log.d("Webservice", " response " + str);
} catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
it will work

SOAP webservice calling using Ksoap

I am trying to call a soap which looks like this in SOAPUI. It's having 4 parameter. url is - http://seycel.com.mx/ws/res2.php
Inputs are like this-
`<usuario xsi:type="xsd:string">1212121212</usuario>
<sms xsi:type="xsd:string">saldo</sms>
<palabra xsi:type="xsd:string">0439267236</palabra>
<fecha xsi:type="xsd:string">2015-05-20 20:10:10</fecha>`
I want to call this from android and fetch the return tag. What I am trying to do is like this -
private static final String SOAP_ACTION = "urn:recargas#saldo";
private static final String METHOD_NAME = "saldo";
private static final String NAMESPACE = "urn:recargas";
private static final String URL = "http://seycel.com.mx/ws/res2.php?wsdl";
private class UserRegistrationTask extends AsyncTask<String, String, String> {
protected String doInBackground(String... values) {
SoapPrimitive result = null;
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("palabra", "0439267236");// Parameter for Method
request.addProperty("usuario", "1212121212");// Parameter for Method
request.addProperty("sms", "saldo");// Parameter for Method
request.addProperty("fecha", "15-05-30 20:52:20");// Parameter for Method
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
result = (SoapPrimitive) envelope.getResponse();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return result.toString();
}
protected void onPostExecute(String result) {
Log.d("TAG", "value: " + result);
}
}
getting an error like this java.lang.String cannot be cast to org.ksoap2.serialization.SoapPrimitive

WSDL calling from Android causes NetworkOnMainThreadException

Here is this sample code (MainActivity):
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.text1);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
tv.setText("Status: " + resultString);
} catch (Exception e) {
tv.setText("Problem: " + e.toString());
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
When I run this code I got this message Problem: android.os.NetworkOnMainThreadException.
In the manifest.xml added the
<uses-permission android:name="android.permission.INTERNET" />
line.
Here
is the whole android project.
Does someone have any idea how to change this code?
Thank you in advance for any help you can provide.
This exception is thrown when an android application attempts to perform a network operation on its main thread. You must run your code in AsyncTask. Your code will look something like this
You can declare resultString as an instance variable
class SoapTask extends AsyncTask<String, Void, Void> {
private Exception exception;
#Override
protected Void doInBackground(Void... arg0) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
resultString = "Status:" + (SoapPrimitive)soapEnvelope.getResponse();
} catch (Exception e) {
resultString = "Problem:" + e.toString();
}
}
protected void onPostExecute(Void result) {
tv.setText("Status: " + resultString);
}
}
Try this..
NetworkOnMainThreadException throws when you are performing network operation on its main thread.
So you have to run you network operations in AsyncTask
You can call AsyncTask like below
new SerializationConnection().execute(URL);
then SerializationConnection class
class SerializationConnection extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(urls[0]);
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
return "Status: " + resultString;
} catch (Exception e) {
return "Problem: " + e.toString();
}
}
protected void onPostExecute(String result) {
tv.setText(result);
}
}
You are making a server call in main thread.if your target version is 9
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
For Better way try to use AsynTask

Error to connect with a web service in android.. HTTP reqest failed,HTTP status 400

I have this code to call a donNet web servise on localhost. The web service take 5 values and return a string.. I took this error: Error HTTP request failed, HTTP status 400.. What is the problem here?
public class MainActivity extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://10.0.2.2:6371/Service1.asmx";
int IDocID;
String SName,SDate,STime,SReason;
TextView txt;
private PropertyInfo AppDoctor,AppDate,AppTime,AppName,AppReason;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sent=(Button)findViewById(R.id.button1);
txt=(TextView)findViewById(R.id.textView6);
sent.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
EditText DocID=(EditText)findViewById(R.id.editTextDoc);
IDocID = Integer.parseInt(DocID.getText().toString());
EditText Date= (EditText)findViewById(R.id.editTextDate);
SDate = Date.getText().toString();
EditText Time=(EditText)findViewById(R.id.editTextTime);
STime = Time.getText().toString();
EditText Name=(EditText)findViewById(R.id.editTextName);
SName = Name.getText().toString();
EditText Reason=(EditText)findViewById(R.id.editTextReason);
SReason = Reason.getText().toString();
new InsertTask().execute();
}
});
}
private String doInsert(String SName,String SDate,String STime,String SReason, int IDocID ) {
String result="";
final String SOAP_ACTION = "http://tempuri.org/InsertAppointment";
final String METHOD_NAME = "InsertAppointment";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("AppDoctor",IDocID);
request.addProperty("AppDate",SDate);
request.addProperty("AppTime",STime);
request.addProperty("AppName",SName);
request.addProperty("AppReason",SReason);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
System.out.println(request);
System.out.println(envelope.toString());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.i("myApp", response.toString());
/
if(response != null)
{
String resp= response.toString();
result = resp;
txt.setText(resp);
}
}catch(SocketException ex)
{
Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
ex.printStackTrace();
txt.setText(ex.toString());
}
catch (Exception e) {
Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
e.printStackTrace();
}
return result;
}
private class InsertTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Logging in...");
this.dialog.show();
}
protected Void doInBackground(final Void... unused) {
String auth=doInsert( SName, SDate, STime, SReason, IDocID);
System.out.println(auth);
return null;// don't interact with the ui!
}
protected void onPostExecute(Void result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
Try to add "" to your SOAP_ACTION:
final String SOAP_ACTION = "\"http://tempuri.org/InsertAppointment\"";

Categories

Resources