An sample application on calling the webservices from android, which is returnig the error, "Unfortunately,MyTest has stopped". -
i have code, makes call web service provided www,tempuri.org. while trying run application, application stopping unexpectedly , showing message - "unfortunately, mytest has stopped".. here java file -
mainactivity.java :-
package com.example.mytest; import org.ksoap2.soapenvelope; import org.ksoap2.serialization.soapobject; import org.ksoap2.serialization.soapprimitive; import org.ksoap2.serialization.soapserializationenvelope; import org.ksoap2.transport.httptransportse; import android.os.bundle; import android.app.activity; import android.view.menu; import android.widget.textview; public class mainactivity extends activity { public static final string soap_action = "http://tempuri.org/celciustofarenheit"; public static final string method_name = "celciustofarenheit"; public static final string namespace= "http://tempuri.org/"; public static final string url = "http://www.w3schools.com/webservices/tempconvert.asmx"; textview tv; soapenvelope se; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv= (textview) findviewbyid(r.id.textview01); soapobject request=new soapobject(namespace,method_name); request.addproperty("celcius","32"); soapserializationenvelope sse = new soapserializationenvelope (se.ver11); sse.dotnet=true; sse.setoutputsoapobject(request); httptransportse abt = new httptransportse(url); try { abt.call(soap_action,se); soapobject resultstring=(soapobject)sse.getresponse(); string resultdata=resultstring.getproperty(0).tostring(); tv.settext("status :"+resultstring); } catch(exception e) { e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
activity_main.xml :-
<relativelayout 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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <textview android:id="@+id/textview01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </relativelayout>
the logcat follows -
08-10 01:35:10.421: e/androidruntime(1077): fatal exception: main 08-10 01:35:10.421: e/androidruntime(1077): java.lang.noclassdeffounderror: org.ksoap2.serialization.soapobject 08-10 01:35:10.421: e/androidruntime(1077): @ com.example.mytest.mainactivity.oncreate(mainactivity.java:30) 08-10 01:35:10.421: e/androidruntime(1077): @ android.app.activity.performcreate(activity.java:5133) 08-10 01:35:10.421: e/androidruntime(1077): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 08-10 01:35:10.421: e/androidruntime(1077): @ android.app.activitythread.performlaunchactivity(activitythread.java:2175) 08-10 01:35:10.421: e/androidruntime(1077): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) 08-10 01:35:10.421: e/androidruntime(1077): @ android.app.activitythread.access$600(activitythread.java:141) 08-10 01:35:10.421: e/androidruntime(1077): @ android.app.activitythread$h.handlemessage(activitythread.java:1256) 08-10 01:35:10.421: e/androidruntime(1077): @ android.os.handler.dispatchmessage(handler.java:99) 08-10 01:35:10.421: e/androidruntime(1077): @ android.os.looper.loop(looper.java:137) 08-10 01:35:10.421: e/androidruntime(1077): @ android.app.activitythread.main(activitythread.java:5103) 08-10 01:35:10.421: e/androidruntime(1077): @ java.lang.reflect.method.invokenative(native method) 08-10 01:35:10.421: e/androidruntime(1077): @ java.lang.reflect.method.invoke(method.java:525) 08-10 01:35:10.421: e/androidruntime(1077): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 08-10 01:35:10.421: e/androidruntime(1077): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 08-10 01:35:10.421: e/androidruntime(1077): @ dalvik.system.nativestart.main(native method)
i new develop web services android. can please suggest me, this?
use this:
xml:
<relativelayout 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" > <button android: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" /> <textview android: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:
public class mainactivity extends activity { button web_service; textview fetch_service; @override public void oncreate(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(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub myasynctask mat = new myasynctask(); mat.execute(); } }); } class myasynctask extends asynctask<string, void, string> { string str; 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"; @override protected string doinbackground(string... params) { soapobject request = new soapobject(namespace, method_name); request.addproperty("celsius", "32"); soapserializationenvelope envelope = new soapserializationenvelope( soapenvelope.ver11); envelope.dotnet = true; envelope.setoutputsoapobject(request); httptransportse ht = new httptransportse(url); try { ht.call(soap_action, envelope); final soapprimitive response = (soapprimitive) envelope .getresponse(); str = response.tostring(); } catch (exception e) { e.printstacktrace(); } log.d("webrespone", str); return str; } @override public void onpostexecute(string result) { // todo auto-generated method stub toast.maketext(mainactivity.this, result, toast.length_long).show(); fetch_service.settext(result); super.onpostexecute(result); } @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); } } }
manifest: add following thins in manifest
<uses-permission android:name="android.permission.internet"/>
output:
hope you.
Comments
Post a Comment