Not Able To Connect To Webservice With Android - android

Not Able To Connect To Webservice With Android
it not acess the webservice code
i have tried to make thread and then run my code there
i test the webservice link from my phone , its reachable
here are the code both .net and android :-
Webservice Code .net :strong text
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
<WebService(Namespace:="http://tempuri.org/")> _
Public Class Service
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As Integer
Return 0
End Function
End Class
Here Is the Code
public class MainActivity extends Activity implements OnClickListener {
ImageButton btn1;
ImageButton Save;
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String MethodName = "HelloWorld";
private static final String NameSpace = "http://tempuri.org/";
private static final String URL = "http://172.20.10.2/WebSite9/Service.asmx";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Thread webser = new Thread() {
public void run() {
try {
// String project = titles.get(position - 1);
// CallWebService();
SoapObject request = new SoapObject(NameSpace, MethodName);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
// request.addProperty("TextToDisplay", "This is coming from android");
envelope.setOutputSoapObject(request);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.getResponse();
// Toast.makeText(MainActivity.this,result.toString(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
};
webser.start();
please help!

Your Code Seems to be ok
please make sure that : -
1- use ksoap2-android-2.5.2.jar
2- go to buildpath>add external jars and select it
3- copy the ksoap2-android-2.5.2.jar and paste it under libs file
restart the application and try ?

put this code:
SoapObject result = (SoapObject)envelope.bodyIn;
instead of this
SoapObject result = (SoapObject)envelope.getResponse();
and it will work with you as expected.

Related

Android Web Service connenction Error

I created Java web services and trying to connect with android code.I am getting the service running but android app is showing error in soap object creating for namespace and method name.I did all the changes and everything is correct namespace,method name,URL all is correct.But I don't know what is wrong can anyone help me out...!
My android code is----->
showing error in
-"SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);"
public class RetailerActivity extends Activity {
private static final String SOAP_ACTION = "urn:training/searchCompanyInfo";
private static final String METHOD_NAME = "searchCompanyInfo";
private static final String NAMESPACE = "urn:training/";
private static final String URL = "http://localhost/attest/CompanyInfoService?wsdl";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println(NAMESPACE);
System.out.println(METHOD_NAME);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
try {
ht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
SoapPrimitive s = response;
String str = s.toString();
String resultArr[] = str.split("&");//Result string will split & store in an array
TextView tv = new TextView(this);
for(int i = 0; i<resultArr.length;i++){
tv.append(resultArr[i]+"\n\n");
}
setContentView(tv);
} catch (Exception e) {
e.printStackTrace();
}
}
}
In Android, You cannot perform network operation on the main thread, You need to execute your soap request in a background thread. Please read up on AsyncTask.

SOAP web service call from android

Could anyone please help in processing the SOAP request and response from an android application.
I have followed the tutorial at
http://www.c-sharpcorner.com/UploadFile/88b6e5/how-to-call-web-service-in-android-using-soap/
But when I run the application, it exits prematurely with the error
Unfortunately, has stopped.
I am running it in an emulator.
Can anyone please explain what I might be missing. Any example source code will be very useful.
I have one simple code for you. Where we have a button and we will be calling helloworld method.
Button Worldmethod;
private static final String SOAP_ACTION1 = "http://tempuri.org/HelloWorld";
private static final String HelloWorldmethod = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.1.5/MobileDemo/Demo.asmx";
#Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.webservices_layout);
Worldmethod= (Button)findViewById(R.id.Worldmethod);
Worldmethod.setOnClickListener(this);
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.Worldmethod:
try
{
methodbody= (TextView)findViewById(R.id.methodbody);
SoapObject request1 = new SoapObject(NAMESPACE, HelloWorldmethod);
// add paramaters and values
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request1);
//Web method call
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION1, envelope);
//get the response
SoapPrimitive result= (SoapPrimitive)envelope.getResponse();
String results = result.toString();
methodbody.setText( ""+results);
}
catch (Exception e)
{
methodbody.setText(e.getMessage());
e.printStackTrace();
}
break;
}
}

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

Consuming WCF service through KSOAP2 on Android gives errors

I have the following WCF webservice
I want to consume it in android client.My code so far is
public class SOAP_TestActivity extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://50.19.226.15/Office365Admin/MsOnline.svc";
private final String SOAP_ACTION = "http://tempuri.org/IMsOnline/ValidateUser";
private final String METHOD_NAME = "ValidateUser";
private final String user_id= "khurram#office365trial.com.au";
private final String password= "friday#123";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button signin = (Button) findViewById(R.id.regsubmitbtn);
signin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
boolean auth=doLogin();
System.out.println(auth);
Toast.makeText(getApplicationContext(), String.valueOf(auth), Toast.LENGTH_SHORT).show();
}
});
}
private boolean doLogin() {
boolean result=false;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("userid", user_id);
request.addProperty("password",password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
Log.d("myApp", request.toString());
System.out.println(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive soapPrimitiveResults = (SoapPrimitive)envelope.getResponse();
Log.d("myApp", soapPrimitiveResults.toString());
System.out.println("myApp" +soapPrimitiveResults);
if(soapPrimitiveResults.toString().equalsIgnoreCase("true"))
{
result = true;
}
}catch(SocketException ex)
{
Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
ex.printStackTrace();
}
catch (Exception e) {
Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
e.printStackTrace();
}
return result;
}
}
This code works without any exception but I am not able to login into my application as the method ValidateUser always return false.This same webservice has been consumed in Windows Phone and is working without any problem.Also the username and password are 100% correct.
Looks like there is some problem in the NAMESPACE and SOAP_ACTION. I have gone through some tutorials and they say that the SOAP_ACTION is made by concatenating NAMESPACE with METHOD_NAME.So if I follow the tutorials my SOAP_ACTION should be
"http://tempuri.org/ValidateUser"
but the SOAP_ACTION given in the WSDL linkfor the method "ValidateUser" is
"http://tempuri.org/IMsOnline/ValidateUser".
I am new to WCF Webservices and SOAP.Can anybody please tell me how to implement the methods given in WSDL Document(link is at the top).
Looks like missing something important but don't know what.
If the same web service works fine with windows phone, then you have a NameSpace problem,
and the web service method is not getting the parameters from the SOAP message. And since your ValidateUser gets no parameters, it just returns null/false.
I had the same problem. I solved it by explicitly defining a namespace hence getting rid of the default namespace WCF sets, tempuri.org
Here is my solution for reference:
passing objects to wcf soap service from android using ksoap2; it sends and receives 0
Try this:
...
Object response = envelope.getResponse();
String value = response.toString();
...

How do I use parameters with ksoap2?

I have created a web service to test passing parameters via ksoap2. I thought it seemed like a pretty simple process, but I'm apparently missing something simple. The web service does nothing more than return the integer that is passed to it. When I execute the code below it returns the number 0 instead of 1.
private static String SOAP_ACTION = "http://tempuri.org/TestParams";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "TestParams";
private static String URL = "http://services.lockrem.com/WebService.asmx?WSDL";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("RoundId", 1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (Exception e) {
e.printStackTrace();
}
SoapObject result = (SoapObject)envelope.bodyIn;
Note: I have tested this using a .net web page and it returns the number 1 as expected. The web service is not the issue here.
Here is the web service.
[WebMethod]
public int TestParams(int RoundId)
{
return RoundId;
}
Thank you for any help.
Disclaimer: Whenever I try to talk to a .NET webservice using kSoap2, I usually end up doing a lot of fiddling. And I mean that literally: I setup a windows machine with Fiddler2 and use that as a proxy for both the failing request and a working reference request, so I can actually see what's going wrong. I strongly advise doing something similar as soon as you try to do something non-trivial.
That said, in your case, it might be enough to do
envelope.dotNet = true;
Try removing the WSDL at the end
private static String URL = "http://services.lockrem.com/WebService.asmx";
In addition try using this to get the response:
Object response = envelope.getResponse();
instead of SoapObject result = (SoapObject)envelope.bodyIn;
EDITED
This works:
public class MainActivity extends Activity {
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "TestParams";
private static String URL = "http://services.lockrem.com/WebService.asmx";
private TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.tv);
HttpTransportSE transport = new HttpTransportSE(URL);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("RoundId", 1);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
try {
transport.call(NAMESPACE + METHOD_NAME, envelope);
Object response = envelope.getResponse();
tv.setText(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Hope it helps.

Categories

Resources