Android Soap Web service Error behind Proxy Server - android

I have written a program for communicating with a web service and get response value. But when i debug the programme i end with requestDump=null at the line androidHttpTransport.call(SOAP_ACTION, envelope); Can some one tell me the reason for the error and what can i do for this
public class WebService extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";
String celsius;
Button b;
TextView tv;
EditText et;
String res,resultval;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_service);
et=(EditText)findViewById(R.id.editText1);
tv=(TextView)findViewById(R.id.Result);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//String result=getFarenheit(et.getText().toString());
//tv.setText(result+"°F");
new service().execute();
}
});
}
private class service extends AsyncTask<Void, Void, String>{
#Override
protected String doInBackground(Void... arg0) {
celsius=et.getText().toString();
SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo celsuiusPI= new PropertyInfo();
celsuiusPI.setName("Celsius");
celsuiusPI.setValue(celsius);
celsuiusPI.setType(String.class);
request.addProperty("XMLMarks",celsuiusPI);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope (SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes = true;
envelope.enc = SoapSerializationEnvelope.ENC2003;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
envelope.setOutputSoapObject(request);
envelope.setAddAdornments(false);
SoapPrimitive response;
HttpTransportSE androidHttpTransport=new HttpTransportSE(URL);
try{
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
String dump= androidHttpTransport.requestDump.toString();
response=(SoapPrimitive)envelope.getResponse();
Toast.makeText(WebService.this, response.toString(), 20).show();
Log.i("WebService output", response.toString());
System.out.println("WebService Response"+response.toString());
Object res= response.toString();
resultval=(String) res;
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h){
String result=h;
tv.setText(result+"°F");
}
}
}

Just replace your service AsyncTask with this new one and see result:
code:
private class service extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... arg0) {
System.out.println("In DoIn Background");
// Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Use this to add parameters
request.addProperty("Celsius", txtCel.getText().toString());
// Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
// Get the first property and change the label text
// txtFar.setText(result.getProperty(0).toString());
res = result.getProperty(0).toString();
} else {
Toast.makeText(getApplicationContext(), "No Response",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h) {
String result = h;
tv.setText(result + "°F");
}
}

Related

Response from SOAP webservice always returns null Android studio

Im trying to get some response from several SOAP webservices at last i tried to run a code that is a well very known example on the internet. But i realized that even this doesnt run on my project. I hardly tried to understand what the error could be but i dont know why its not working with soap.
I would really appriacate your help.
Downloaded new version of KSOAP2 and also permission for internet is given.
public class WEBSERVİCE extends AppCompatActivity {
Button btn;
EditText et;
TextView txv;
String celcius="21";
String fahren;
private String NAMESPACE = "https://www.w3schools.com/xml/";
private String METHOD_NAME = "CelsiusToFahrenheit";
private String SOAP_ACTİON = "https://www.w3schools.com/xml/CelsiusToFahrenheit";
private String URL = "https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit?WSDL";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.button);
txv = findViewById(R.id.textView);
et = findViewById(R.id.editText1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
});
}
private class AsyncCallWS extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
txv.setText("calculating");
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
#Override
protected String doInBackground(String... objects) {
return getBolum(celcius);
}
#Override
protected void onPostExecute(String o) {
txv.setText(fahren + "F");
super.onPostExecute(o);
}
}
public String getBolum(String celsius) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("Celcius");
pi.setValue(celsius);
pi.setType(double.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHTTPTransport = new HttpTransportSE(URL);
try {
androidHTTPTransport.call(SOAP_ACTİON, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
fahren = response.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return fahren;
}
}
No Error Messages but the value it turns back is always "null"
EDIT:posted changed code again
There isn't anything wrong with SOAP Api. The problem is your AsyncTask class. Read the documentation for AsyncTask first. Please do proper research before you use any code from internet. Always read about the components that are used snippets on internet otherwise you are going to have hard time figuring out problems.
Your AsyncTask class is declared as:
private class AsyncCallWS extends AsyncTask<String,Void,Void>
Change it to
private class AsyncCallWS extends AsyncTask<String,Void,String>
Third generic parameter in your Void which is supposed to be result type. So in your case your async task won't return any data once it is finished.
//CHANGE TYPE TO STRING
public String getBolum(String celsius) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("Celcius");
pi.setValue(celsius);
pi.setType(double.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHTTPTransport = new HttpTransportSE(URL);
try {
androidHTTPTransport.call(SOAP_ACTİON, envelope);
SoapPrimitive response= (SoapPrimitive) envelope.getResponse();
//RETURN RESULT
return response.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
//I forgot this line previously:
return "";
}}
In your async task, you need to change return type of doInBackground and paramter of onPostExecute to String:
#Override
protected String doInBackground(String... objects) {
return getBolum(celcius); //RETURN RESULT
}
#Override
protected void onPostExecute(String result) {
txv.setText(result+"F");
fahren = result;
super.onPostExecute(result);
}
It should work now.

I'm Unable to setText of a TextView in a class (Android)

I'm using SOAP, In AsyncTask classs I'm trtying to set text of driverIDText, shipmentIDText, freightShipment textviews but it's saying unable to resolve setText()
Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_up);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView driverIDText = (TextView) findViewById(R.id.text_DriverID);
TextView shipmentIDText = (TextView) findViewById(R.id.text_ShipmentID);
TextView freightShipment = (TextView) findViewById(R.id.text_FreightShipment);
new RetrieveFeedTask(driverIDText.getText.toString(),
shipmentIDText.getText().toString(),
freightShipment.getText().toString()
).execute();
}
class RetrieveFeedTask extends AsyncTask<String, String, Void> {
private String driverIDText, shipmentIDText, freightShipment;
RetrieveFeedTask(String driverIDText, String shipmentIDText, String freightShipment) {
this.driverIDText = driverIDText;
this.shipmentIDText = shipmentIDText;
this.freightShipment = freightShipment;
}
protected Void doInBackground(String... urls) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("DriverID", driverIDText);
request.addProperty("ShipmentID", shipmentIDText);
request.addProperty("FreightShipment", freightShipment);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
// HERE IS PROBLEM IN SETTEXT()
driverIDText.setText(result.getProperty(0).toString());
} else {
Toast.makeText(getApplicationContext(), "Login Failed!", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Inside AsyncTask you declared driverIDText as String not TextView, So Create one more param for TextView and send driverIDText(TextView) from Oncreate and set the data in textView in onPostExecute.
Ex:
class RetrieveFeedTask extends AsyncTask<String, String, String> {
private String driverIDText, shipmentIDText, freightShipment;
private TextView driverIDTextTv;
RetrieveFeedTask(String driverIDText, String shipmentIDText, String freightShipment, TextView textView) {
this.driverIDText = driverIDText;
this.shipmentIDText = shipmentIDText;
this.freightShipment = freightShipment;
driverIDTextTv = textView;
}
#Override
protected void onPostExecute(String text) {
super.onPostExecute(text);
driverIDTextTv.setText(text);
}
protected String doInBackground(String... urls) {
String localDriverId = "";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("DriverID", driverIDText);
request.addProperty("ShipmentID", shipmentIDText);
request.addProperty("FreightShipment", freightShipment);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
// HERE IS PROBLEM IN SETTEXT()
// driverIDText.setText(result.getProperty(0).toString());
localDriverId = result.getProperty(0).toString();
} else {
Toast.makeText(getApplicationContext(), "Login Failed!", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return localDriverId;
}
}
and you can call AsyncTask like this,
new RetrieveFeedTask(driverIDText.getText.toString(),
shipmentIDText.getText().toString(),
freightShipment.getText().toString(), driverIDText
).execute();

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

Categories

Resources