Since I'm using "SoapSerializationEnvelop" to use web services in my app, I must have a special jar file called "ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar". you can download it from here. Go to this link and click "View raw file". Then you can download the file. http://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.2/ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar?r=498
Then copy the file in to this directory. "C:\Program Files\Android\android-sdk\platforms\android-10". Now you are done with that.
Now go to the android project in eclipse. Right click the project and go to properties. Select "Java Build Path", select "Libraries", click "Add External JARs", select the .jar file and press ok.
Ok. Now you have to give INTERNET permission for your app. To do that, in your manifest file write the uses-permission tag like this at bottom.
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
Then I modify the file like below. In here, to connect to the web server I'm using "SoapSerializationEnvelop" and "HttpTransportSE" classes.
public class Login extends Activity {
private static final String SOAP_ACTION = nhttp://tempuri.org/memberValidation";
private static final String METHOD_NAME = nmemberValidationn;
private static final String MAMESPACE = "http://tempuri.orgr;
private static final String URL = nhttp://10.0.2.2:8833/WebService/Service.asmx";
EditText name;
EditText pass;
TextView tv;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.Login);
name.(EditText)findViewById(R.id.txtname);
pass.(EditText)findViewById(R.id.txtpassword);
tv.(TextView)findViewById(R.id.txtError);
Button btnSignIn.(Button)findViewById(R.id.htnsignin);
btnSignIn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
SoapObject Request = new SoapObject(NAMESPACE, METHOD NAME);
Request.addProperty("name" , name.getText().toString());
Request.addProperty("passwd", pass.getText().toString());
SoapSerializationEnvelope soapEnvelop;
soapEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelop.dotNet = true;
soapEnvelop.setOutputSoapObject(Request);
HttpTransportSE htp = new HttpTransportSE(URL);
try{
tv.setText("");
htp.call(SOAPACTION,soapEnvelop);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelop.getResponse();
String id = resultString.toString();
Intent intent = new Intent(Login.this,Welcome.class);
startActivity(intent);
catch
(Exception e){
tv.setText("Invalid details. Please check again!");
});
This is the most important part.
The four string in above are defined according to your web service web page.
METHOD_NAME - your method name (" memberValidation ")
NAMESPACE - "http://tempuri.org/"
URL - replacing 'localhost' by '10.0.2.2' .
Important :
The first parameter name of Request.addProperty method should be equal to parameter name in memberValidation method. Otherwise it won't work.
If everything is fine user redirect to the welcome page.
Run it in emulator and see how nice it is.
Make sure your web service is running..
Congratulation !
We are done with the web services in our app.
In my next chapter I'll explain how to pass a value from one activity to another.
See you in the next chapter.
First, this very good. But, String Id is not user in you code, can you explain plz.
ReplyDeleteyeah! In user log into the app it checks the id is available. If it is not available an exception will be occurred in this line
Delete'String id = resultString.tostring();'
Then it jumps to catch block and display an error message. If id is available (user is there) then it'll redirect to welcome page.
my app is not working... its saying unfortunately, solutionDude has stopped working.. can u plz guide me what actually is causing this problem...
ReplyDeleteyeah , I think there is an exception in your program. Did you add the activity to your manifest file ?
Deletesee here http://1.bp.blogspot.com/-TGhz6ealNxw/T3HGYLDUBNI/AAAAAAAAANA/XAZ_aM-Qi7M/s1600/16.png
im trying to access real live website with web services, do I need to add the port in the URI or just http://website.com/ws/webservis.asmx
ReplyDeleteNo. you don't need the port number for real live websites. It'll be needed only when you run it using localhost.
DeleteHi Mihiran Rupasinghe,
ReplyDeleteI follow your tutorial to test my own app but it appears an error at "btnButton.setOnClickListener(new OnClickListener() {" .Would you like to tell me why? Thank in advance
can you please tell me what is the error message ?
Deletecan you please post your code inside "btnButton.setOnClickListener(new OnClickListener() { " ?
DeleteHi,
ReplyDeleteHow do you make the logout work?
And alsow, when i log inn, and if i paste wrong username or password, the intent still runs.
ReplyDelete