开发者

Help needed in writing syntax in android

I am facing some problems in Android as I am new to it. I need to know how to write syntax in Android for the below problem.

Problem: I have a .net web service (www.somesite.com). That webserver has an authentication method which requires a user name and a password as a parameter to authenticate. Once I set those things using the authentication method, it will allow me to call the rest of the functionality present in the webserver. I have the source code written in ASP. I want to write the same code in Android.

 private MyServerAPI.Service _service;
 _service = new MyServerAPI.Service();

MyServerAPI.DTAuthHeader auth = new MyServerAPI.DTAuthHeader();
auth.Username = ConfigurationManager.AppSettin开发者_开发百科gs["MyServerAPI.user"];
auth.Password = ConfigurationManager.AppSettings["MyServerAPI.pass"];

_service.DTAuthHeaderValue = auth;
_service.Url = ConfigurationManager.AppSettings["MyServerAPI.service"];

Basically, I want to write the same thing as the above code in Android.


There's a library for dealing with WebServices called KSOAP2. find it here

Use it and notice how your server requrires the username and password to be sent.

UPDATE
Code Snippet:

SoapObject soapRequest = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME) ;
    soapRequest.addProperty("appName","MyCoolApp" );
    soapRequest.addProperty("sEmail","test@example.com" );
    soapRequest.addProperty("sPassword","test" );
    SoapSerializationEnvelope soapEnvelope = new     SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapEnvelope.dotNet  = true;
    soapEnvelope.setOutputSoapObject(soapRequest);

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

    try{
    httpTransport.call(SOAP_ACTION, soapEnvelope);
    Object response = soapEnvelope.getResponse();
    Log.d("-----RESPONSE",""+response);
    }catch (Exception exception){
    Log.d("RESPONSE",""+exception.toString());
    }

Although I discourage posting whole code here, but since you seem to be in dire trouble, I hope this much helps you.

Or wait a bit I'll blog about it, in detail.


If you dont want to depend on 3rd party library then you call also use the inbuild class of android itself.

Here is an function which is used to post data on your webserver along with passing username and password.

public void postDataToWeb() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("www.yoursite.com");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("Username", "Paresh"));
        nameValuePairs.add(new BasicNameValuePair("Password", "PM@Android"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

Now, you are having response in inputstream (in above example, 'is') and you can play with inputstream.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜