cant parse xml using a KSOAP2 - android

I have connected to the asp.net Service using Ksoap2 and it connects fine, but one thing is that i get the response back in XML. Is there anyway i can get it to display in normal text.
This is the code i have used
public class AndroidWebService extends Activity {
/** Called when the activity is first created. */
private static String SOAP_ACTION = "http://tempuri.org/GetHelpDeskCalls";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "GetHelpDeskCalls";
static final String URL = "https://198.125.364:8080/AndroidServices/Service1.asmx";
Button getData;
EditText userID;
TextView data;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.helpdesk);
getData = (Button) findViewById(R.id.button1);
userID = (EditText) findViewById(R.id.txtFar);
data = (TextView) findViewById(R.id.textView1);
Thread nT = new Thread() {
#Override
public void run() {
getData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SoapObject request = new SoapObject(NAMESPACE,
METHOD_NAME);
request.addProperty("userID", userID.getText()
.toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(
URL);
// androidHttpTransport.call(SOAP_ACTION, envelope);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
final String ss = androidHttpTransport.responseDump;
// final SoapObject response = (SoapObject) envelope
// .getResponse();
runOnUiThread(new Runnable() {
public void run() {
data.setText(ss.toString());
}
});
} catch (Exception e) {
data.setText("Error");
}
}
});
}
};
nT.start();
}
}

There is a neat website called google, it does wonders.
since you seem to be lazy there are 3 basic xml parsing methods you can use
SAX parser,
DOM parser,
XML pull parser
read about xml parsing here

Related

Unable to call Java Webservice from android application

I am trying to call java webservice form android application but unable to call it. When I generate WSDL file, SOAPAction showing blank
string(<soap:operation soapAction=""/>) in soap:operation.
My android application code:
public class MainActivity extends Activity {
private static final String SOAP_ACTION = "http://com/add";
private static final String METHOD_NAME = "add";
private static final String NAMESPACE = "http://com/";
private static final String URL = "http://localhost:8080/WebApplication1/demo";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi1 = new PropertyInfo();
pi1.setName("i");
pi1.setValue(3);
pi1.setType(int.class);
request.addProperty(pi1);
PropertyInfo pi2 = new PropertyInfo();
pi2.setName("j");
pi2.setValue(3);
pi2.setType(int.class);
request.addProperty(pi2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
// HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
try {
((TextView) findViewById(R.id.textView1)).setText(String
.valueOf("The WebService is about to call"));
androidHttpTransport.call(SOAP_ACTION, envelope);
((TextView) findViewById(R.id.textView1)).setText(String
.valueOf("The WebService call is done"));
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
((TextView) findViewById(R.id.textView1)).setText(String
.valueOf(response.toString()));
((TextView) findViewById(R.id.textView1)).setText(String
.valueOf("Done"));
} catch (Exception e) {
e.printStackTrace();
Log.e("Err", "Error Says: " + e.toString());
}
}
});
}
}
There is showing an exception network on main thread exception
Because you are calling the web service in your main thread.This must be done in the background using background thread.You can use asyntask for background operations.

Ksoap2 .NET web service connection

http://i.stack.imgur.com/pK9rm.png
http://i.stack.imgur.com/27Qj0.png
I want to connection web service using ksoap2,I have a working example but how can I integrate my application?
public class WebServiceDemoActivity extends Activity
{
private static String SOAP_ACTION = "http://tempuri.org/UrunleriListele";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "UrunleriListele";
private static String URL = "http://services.annebebekavm.com/Service1.asmx?WSDL";
Button btnFar,btnCel,btnClear;
EditText txtFar,txtCel;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.btnFar);
txtFar = (EditText)findViewById(R.id.txtFar);
btnFar.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Fahrenheit",txtFar.getText().toString());
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)
{
txtFar.setText(result.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Please reconsider to use another client
http://code.google.com/p/android-ws-client/
you can create all classe what you need to.
by one application copy libs to your project
with your generate classes
Here is example it look really simple to
Service servis = new Service();
ServiceSoap soap12 = servis.getServiceSoap12();
LoginParams inParams = new LoginParams();
inParams.setLogin("user");
inParams.setPassword("pass");
inParams.setStrategy(LoginStrategy.NOHASHPASS); // even enums are supported
LoginState result = soap.authorize(inParams);
result.isLogin(); // boolean if is succesfull

Showing result in tabhost on android eclipse

The above image is just adding two values and displaying the result on the below textview.
But i need to show that answer on the first tab(ie.Tab_1) of tabhost on next screen.
How to do this?
.java class
public class Demo_webserviceActivity extends Activity
{
/** Called when the activity is first created. */
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "FahrenheitToCelsius";
private static String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar;
EditText txtFar;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.btnFar);
txtFar = (EditText)findViewById(R.id.txtFar);
btnFar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String b;
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Use this to add parameters
request.addProperty("Fahrenheit",txtFar.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.
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
if(result != null)
{
//Get the first property and change the label text
b = result.toString();
Intent i = new Intent(getApplicationContext(),Activity2.class);
i.putExtra("gotonextpage", b.toString());
startActivity(i);
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
NOTE : This source is actually consuming a webservice from android and showing the result by SOAP method
Thanks a lot.
You can use Shared preferences to store values, then you can access it in any activity of your project:
follow this link

Web Service Issue

I am using Soap WEb service but I am facing one problem like this soap:Server' faultstring: 'Server was unable to process request. ---> There is no row at position 0.' faultactor: 'null
My Code is
public class GetData extends Activity {
private static final String SOAP_ACTION="http://xxx.mobi/GetMobileContentMetaData";
private static final String METHOD_NAME="GetMobileContentMetaData";
private static final String NAMESPACE ="http://xxx.mobi/";
private static final String URL = "http://webservices.xxx.mobi/MobileLMSServices.asmx";
SoapObject request;
Button submit;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getdata);
submit=(Button)findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("SiteURL","http://heaklthcare.xxx.mobi/");
request.addProperty("ContentID","62b90739-57b9-47cc-884a-8be2ef265304");
request.addProperty("UserID",809);
request.addProperty("DelivoryMode",2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
SoapObject result = null;
envelope.setOutputSoapObject(request);
AndroidHttpTransport sab = new AndroidHttpTransport(URL);
sab.debug = true;
try {
sab.call(SOAP_ACTION, envelope);
if (envelope.getResponse() != null)
{
result = (SoapObject) envelope.bodyIn;
for (int i1 = 0; i1 < 1; i1++) {
Object property = result.getProperty(i1);
if (property instanceof SoapObject) {
SoapObject countryObj = (SoapObject) property;
Log.d("country" ,"object"+countryObj);
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Should be an error returned by the server. If you can, call the web method from .net (or if you have a localhost copy of the webservice, input parameters in the browser) .
I struggled with this error this morning, then I did a small (5 lines) .net app to call the web method. then i got a clearer error message. Also turned out my java code had no problems the whole time.

Logfile showing empty fields in the server side

i got a problem with my web services code. with the code I am passing the user credentials to server, but at the server side log file showing empty fields I cant understand the problem as I am new to webservices. I am using ksoap ,wsdl for my webservices
Any help would be greatly appreciated...
this is the code
private static final String SOAP_ACTION = "http://tempuri.org/RegisterUser";
private static final String METHOD_NAME = "RegisterUser";
private static final String NAMESPACE = "http://tempuri.org";
private static final String URL = "http://........";
private static final String TAG = "HELLO";
Thread t;
ProgressDialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button register = (Button) findViewById(R.id.regsubmitbtn);
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(0);
t = new Thread() {
public void run() {
register();
}
};
t.start();
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0: {
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait while connecting...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
Button regmalebtn;
public void register() {
Log.v(TAG, "Trying to Login");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
// String response1 = request.getProperty(0).toString() ;
request.addProperty("fname", "raghav");
request.addProperty("lname", "raju");
request.addProperty("dateofbirth", "18-01-1985");
request.addProperty("email", "raghuraju90#yahoo.com");
request.addProperty("password", "1234");
//request.addProperty("password", repassword);
request.addProperty("mobno", "8553456260");
request.addProperty("latitude", "76");
request.addProperty("longitude", "82");
request.addProperty("device_id", "123456");
request.addProperty("gender", "male");
// request.addProperty("latitude',latitude);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
androidHttpTransport
.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
// SoapObject result = (SoapObject) soapEnvelope.getResponse();
Log.v("TAG", String.valueOf(resultsRequestSOAP));
String resultData;
resultData = request.getProperty(0).toString();
}
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
For this you should get a copy of Charles - http://www.charlesproxy.com/
You can configure it to act as a reverse proxy and then if you aim your web service code at the reverse proxy you'll be able to inspect the traffic flowing from your phone to the server. I've outlined the whole process in another thread.
Android to Drupal cookie transfer Q2
If you need help let me know.

Categories

Resources