A Sample Application On Calling The Webservices From Android, Which Is Returning The Error, "unfortunately,mytest Has Stopped"
Solution 1:
Use like this:
XML:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/web_service"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="112dp"android:layout_marginTop="146dp"android:text="Web Service" /><TextViewandroid:id="@+id/fetch"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:text="TextView" /></RelativeLayout>
ACTIVITY:
publicclassMainActivityextendsActivity {
Button web_service;
TextView fetch_service;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fetch_service = (TextView) findViewById(R.id.fetch);
web_service = (Button) findViewById(R.id.web_service);
web_service.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stubmyasynctaskMAT=newmyasynctask();
MAT.execute();
}
});
}
classmyasynctaskextendsAsyncTask<String, Void, String> {
String str;
privatestaticfinalStringSOAP_ACTION="http://tempuri.org/CelsiusToFahrenheit";
privatestaticfinalStringMETHOD_NAME="CelsiusToFahrenheit";
privatestaticfinalStringNAMESPACE="http://tempuri.org/";
privatestaticfinalStringURL="http://www.w3schools.com/webservices/tempconvert.asmx";
@Overrideprotected String doInBackground(String... params) {
SoapObjectrequest=newSoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSEht=newHttpTransportSE(URL);
try {
ht.call(SOAP_ACTION, envelope);
finalSoapPrimitiveresponse= (SoapPrimitive) envelope
.getResponse();
str = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
Log.d("WebRespone", str);
return str;
}
@OverridepublicvoidonPostExecute(String result) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
fetch_service.setText(result);
super.onPostExecute(result);
}
@OverrideprotectedvoidonPreExecute() {
// TODO Auto-generated method stubsuper.onPreExecute();
}
}
}
Manifest: add the following thins in your manifest
<uses-permissionandroid:name="android.permission.INTERNET"/>
Output:
Hope this will help you.
Solution 2:
You cannot do network related operation on the main ui thread. You should use a Thread
or AsyncTask
.
You are doing Network Related operation on the ui thread. You will get NetworkOnMainThredException
in api level 11 and above.
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
AsyncTask docs :
http://developer.android.com/reference/android/os/AsyncTask.html
I suspect you do not have the jar file under libs folder Make sure you have added the ksoap jar file to your libs folder.
You can also use thread but you cannot updte ui from the backgound thread. You should update ui on the ui thread. So asynctask makes it easier.
Example : FahrenHeitToCelsius. Similarly you can call appropriate soap methods for CelciusToFarenheit. Same steps to be followed
publicclassFirstScreenextendsActivity
{
privatestaticStringSOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
privatestaticStringNAMESPACE = "http://tempuri.org/";
privatestaticStringMETHOD_NAME1 = "FahrenheitToCelsius";
privatestaticStringURL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
SoapObject result;
Button b;
EditText et;
int value;
TextView tv;
ProgressDialog pd;
@OverridepublicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
pd= newProgressDialog(this);
pd.setTitle("Making Soap Request");
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.button1);
tv= (TextView) findViewById(R.id.textView2);
et= (EditText) findViewById(R.id.ed);
b.setOnClickListener(newOnClickListener()
{
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stub
value=Integer.parseInt(et.getText().toString());
newone1().execute();
}
});
}
privateclassone1extendsAsyncTask<String, Integer, SoapObject> {
protectedvoidonPreExecute()
{
pd.show();
}
protectedSoapObjectdoInBackground(String... arg0) {
// TODO Auto-generated method stub//Initialize soap request + add parametersSoapObject request = newSoapObject(NAMESPACE, METHOD_NAME1);
//Use this to add parameters
request.addProperty("Fahrenheit",value);
//Declare the version of the SOAP requestSoapSerializationEnvelope envelope = newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = newHttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION1, envelope);
result = (SoapObject)envelope.bodyIn;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
protectedvoidonPostExecute(SoapObject result)
{
pd.dismiss();
if(result != null)
{
tv.setText("Result = "+result.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
}
}
My activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="FahrenheitToCelsius" />
<EditText
android:id="@+id/ed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="43dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="37dp"
android:text="MakeRequest" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Result" />
</LinearLayout>
Lastly make sure you have internet permission in manifest file
<uses-permissionandroid:name="android.permission.INTERNET"/>
Snap Shot.
Solution 3:
Nirmal's answer above did the trick for me. However, I had to update action and namespace as follows:
privatestaticfinalStringSOAP_ACTION="http://www.w3schools.com/webservices/CelsiusToFahrenheit";
privatestaticfinalStringNAMESPACE="http://www.w3schools.com/webservices/";
I figured out what the correct values should be by:
Going to the web service page: http://www.w3schools.com/webservices/tempconvert.asmx
Clicking the service description link: http://www.w3schools.com/webservices/tempconvert.asmx?WSDL
Searching for the strings (case-insensitive) "action" and "namespace" in that xml, and then use the values they have
Solution 4:
I have received the same exception:
java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject at
com.example.soap_ksoap2_lino.MainActivity.onCreate(MainActivity.java:29)
I think that the .jar file from code.google.com/p/ksoap2-android is only for compilation. It has no classes. All other .jars also don't work. I have abandoned the KSOAP2 API strategy, and I try now to use the HttpPost whose .jar is already present in the Eclipse ADT.
Here is my code: Sending SOAP request from Android. Server responses: "Server was unable to process request. "
I receive a response from the w3schools server, however the server responds: "Server was unable to process request".
Post a Comment for "A Sample Application On Calling The Webservices From Android, Which Is Returning The Error, "unfortunately,mytest Has Stopped""