开发者

Android Regarding how to acess soap web services and parse the data using json

开发者 https://www.devze.com 2023-02-15 22:16 出处:网络
i have a piece of code regarding login authentication using soap web services and json parsing but am confused that is it correct or not please guide me ??

i have a piece of code regarding login authentication using soap web services and json parsing but am confused that is it correct or not please guide me ?? if possible please provide me code.

Here is my code :-

package com.devstream.http;
import java.io.*;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
    ImageButton b;
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL ="http:....asmx";
    private static final String SOAP_ACTION = "http://tempuri.org/LoginRequest";
    private static final String METHOD_NAME = "LoginRequest";
    protected static final String END_DOCUMENT =null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        b=(ImageButton)findViewById(R.id.ImageButton01);
        b.setOnClickListener(new OnClickListener()
        { 
            public void onClick(View v) {
                String sUserName,sPassword;
                EditText usernameEditText = (EditText)findViewById(R.id.EditText01);
                EditText passwordEditText = (EditText)findViewById(R.id.EditText02);


        if(usernameEditText == null ||passwordEditText == null){
                    Toast.makeText(getApplicationContext(), "Please Enter Valid Email and Password", Toast.LENGTH_LONG).show();
                    //Log.i("enter Correct details", null);


                }else if(usernameEditText != null || passwordEditText != null){
                    sUserName = usernameEditText.getText().toString();
                    sPassword = passwordEditText.getText().toString();
                    String string = 
                        "{\"Geninfo\": " +
                        "{\"appname\":\"MNB\"," +
                        "\"appver\":\"1.0.0\"," +
                        "\"deviceType\":\"BlackBerry\"," +
                        "\"deviceOSVersion\":\"3.0\":" +
                        "\"phoneDeviceID\":\"9150107470\"}," +
                        "\"Login\":" +
                        "{\"emailID\": " + sUserName +
                        "\"Password\": "+ sPassword + "}}"; 

                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
                    request.addProperty("JsonRequestString",string);

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); 

                    envelope.encodingStyle = SoapSerializationEnvelope.ENC2001; 
                    envelope.dotNet=true; 
                    envelope.setOutputSoapObject(request);

                    Log.i("Given Input",string);
                    Log.i("LoginDetail","Username : " +sUserName+ "Password :" +sPassword); 

                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                    androidHttpTransport.debug=true;

                    System.out.println("request: " + androidHttpTransport.requestDump);
                    System.out.println("response:"+androidHttpTransport.responseDump);
                    androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

                    try {
                        androidHttpTransport.call(SOAP_ACTION, envelope);
                        SoapPrimitive resultstring = (SoapPrimitive) envelope.getResponse();
                        Log.i("OUTPUT",resultstring.toString()); }
                    catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                    next();
                }}//end else
        });
        XmlPullParserFactory开发者_JS百科 factory;
        try {
            factory = XmlPullParserFactory.newInstance();
            XmlPullParser xpp = factory.newPullParser();
            String n=xpp.getText().toString();
            while( n != END_DOCUMENT ) {
                try {
                    // do your parsing element-by-element
                } catch( Throwable e ) {
                    Log.w("error while parsing file: " + e.getMessage(),""); 
                    // you may want to catch any other exceptions and do something about them
                }
            } 
        } catch (XmlPullParserException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace(); 
        }
    }

    public void setUserNameText(String $emailid){
        EditText usernameEditText = (EditText)findViewById(R.id.EditText01);
        usernameEditText.setText($emailid);
    }
    public void setPasswordText(String $password){
        EditText passwordEditText = (EditText)findViewById(R.id.EditText02);
        passwordEditText.setText($password);
    }
    private void next(){ 
        Intent i=new Intent(getApplicationContext(), Home.class);
        startActivity(i);
    }
}

when running this in logcat am able to see input,username and password but it shows request and response null what is the problem am not able to understand please guide me ???


Please follow the below code . Here i send request on the server and getting that response and parse that response in json.

public void Login(final String UserName,final String Password)
{
    try
    {
        new Thread()
        {
            public void run()
            {
                try
                {
                    HttpPost postMethod = new HttpPost("Your Url");
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                    nameValuePairs.add(new BasicNameValuePair("UserName","Your UserName");
                    nameValuePairs.add(new BasicNameValuePair("Password", "Your Password"));

                    postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    DefaultHttpClient hc = new DefaultHttpClient();

                    HttpResponse response = hc.execute(postMethod);
                    HttpEntity entity = response.getEntity();

                    // If the response does not enclose an entity, there is no need
                    // to worry about connection release

                    if (entity != null) 
                    {
                        InputStream inStream = entity.getContent();
                        result= convertStreamToString(inStream);
                        jsonObject = new JSONObject(result);
                        responseHandler.sendEmptyMessage(0);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }.start();

        responseHandler = new Handler() 
        {                               
            public void handleMessage(Message msg) 
            {
                super.handleMessage(msg);
                try 
                {
                    Log.i("-------------------- Response",result);
                    if(jsonObject.getJSONObject("response").getString("msg").equalsIgnoreCase("Success"))
                    {

                        Log.i("Login","--- Login Success");
                    }
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }                       
            }
        };
    }
    catch(Exception e)
    {

    }
}

public static String convertStreamToString(InputStream is)
{
   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
   StringBuilder sb = new StringBuilder();

   String line = null;
   try 
   {
       while ((line = reader.readLine()) != null) 
       {
           sb.append(line + "\n");
       }
   } 
   catch (IOException e) 
   {
       e.printStackTrace();
   } 
   finally 
   {
       try 
       {
           is.close();
       } 
       catch (IOException e) 
       {
           e.printStackTrace();
       }
   }
   return sb.toString();

}

0

精彩评论

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