开发者

Android: (webservices) How to store the data in remote server and retrieve it

I have an Android application which needs username and password to login. I need to save the username and password in a remote server by entering the details in the register page with an alert box showing Registered successfully. When the user opens the app n开发者_如何学JAVAext time, he will login. I want to use client/server mechanism. After I get the response, I want to parse it either using sax parser or soap. I searched a lot through Google, but I didn't find a correct example. As I am new to webservices, I couldn't solve it. Please help me.

EditText input1 = (EditText) findViewById(R.id.usertext);
EditText input2 = (EditText) findViewById(R.id.Passtext);
String username = input1.getText().toString();
String password = input2.getText().toString();


package com.google.android.Test;


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class myWebService extends Activity
{
         private static final String SOAP_ACTION = "HelloYou";
     private static final String METHOD_NAME = "getHello";
     private static final String NAMESPACE = "urn:HelloYou";
     private static final String URL = "http://localhost/lab/service.php";
     private Object resultRequestSOAP = null;

            @Override
            public void onCreate(Bundle icicle)
            {
                super.onCreate(icicle);
                TextView tv = new TextView(this);
                setContentView(tv);


            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);


             //SoapObject
            request.addProperty("firstname", "John");
            request.addProperty("lastname", "Williams");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);


            HttpTransport androidHttpTransport = new HttpTransport(URL);
            try
            {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                resultsRequestSOAP =  envelope.getResponse();
                String[] results = (String[])  resultsRequestSOAP;
                tv.setText( results[0]);
            }
            catch (Exception aE)
            {
                aE.printStackTrace ();;
            }
           }
}


Try this code. I run it successfully on emulator.

I take the source code from with some changes : http://egkatzioura.wordpress.com/2011/07/04/nusoap-and-ksoap2-android-and-php-alliance/

Server.php

<?php


// include NuSOAP library
    require_once('nusoap-0.9.5/lib/nusoap.php');



// Create Web Service Server
    $server = new nusoap_server;
    $server->configureWSDL('aritmatikawsdl', 'urn:aritmatikawsdl');





    $server->register('pollServer',
        array('value' => 'xsd:string'),
        array('return' => 'xsd:string'),
        'urn:aritmatikawsdl',
        'urn:aritmatikawsdl#pollServer',
    'rpc',
    'encoded',
    'Test'
        );



// Define Services


    function pollServer($value){

        if($value['value'] == 'Good'){

            return $value['value'].""."  The value of the server poll resulted in good information";
        }
        else{

            return $value['value'].""."  The value of the server poll showed poor information";
        }
    }


    $server->service($HTTP_RAW_POST_DATA);
?>

KSOAP2

package org.tuxpan;





    import java.io.IOException;
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

    public class Adder extends Activity{


    private static final String SOAP_ACTION = "pollServer";
    private static final String METHOD_NAME = "pollServer";
    private static final String NAMESPACE = "http://10.0.2.2/DroidAssessment/server.php";
    private static final String URL = "http://10.0.2.2/DroidAssessment/server.php?wsdl";

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);

    SoapObject soapclient = new SoapObject(NAMESPACE,METHOD_NAME);
    //Yes you need this one in order to send the whole string or else only the first letter
    //is going to be send
    SoapObject parameters = new SoapObject(NAMESPACE, METHOD_NAME);
    parameters.addProperty("value","Good");
    soapclient.addProperty(METHOD_NAME,parameters);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(soapclient);
    HttpTransportSE httpTransportSE = new HttpTransportSE(URL);

    try {
    httpTransportSE.call(SOAP_ACTION, envelope);

    Log.v("TEST","runs ok attributes "+ envelope.getResponse().toString());

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Log.v("TEST","io wrong");
    } catch (XmlPullParserException e) {
    Log.v("TEST","xml wrong");
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜