2011-06-09 77 views
1

我有一個需要用戶名和密碼登錄的Android應用程序。我需要將用戶名和密碼保存在遠程服務器上,方法是在註冊頁面中輸入詳細信息,並在警告框中顯示Registered successfully。當用戶下次打開該應用程序時,他將登錄。我想使用客戶端/服務器機制。在得到響應之後,我想使用sax解析器或soap解析它。我通過谷歌搜索了很多,但我沒有找到一個正確的例子。由於我是web服務新手,我無法解決它。請幫幫我。Android:(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(); 

回答

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

試試這個代碼。我在模擬器上成功運行它。

予取源代碼從與一些變化: 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(); 
    } 

    } 

    } 
相關問題