Android :( Web服务)如何将数据存储在远程服务器中并检索它

时间:2011-06-09 07:02:59

标签: android web-services

我有一个需要用户名和密码登录的Android应用程序。我需要在注册页面输入详细信息,并在警告框中显示Registered successfully,以便在远程服务器中保存用户名和密码。当用户下次打开应用程序时,他将登录。我想使用客户端/服务器机制。在我得到响应后,我想使用sax解析器或soap解析它。我通过谷歌搜索了很多,但我找不到一个正确的例子。由于我是webservices的新手,我无法解决它。请帮帮我。

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

2 个答案:

答案 0 :(得分:1)

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 ();;
            }
           }
}

答案 1 :(得分:1)

试试这段代码。我在模拟器上成功运行它。

我从一些更改中获取源代码: 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();
    }

    }

    }