Search code examples
javaandroidandroid-ksoap2

Accessing to web service via network


I created an asp.net web service and using emulator I can easily access to the web service what I want now that I want to access to the web service via network so my phone will be the client and my laptop will be server trying to do that for many hours but nothing happened still gave me java.lang.NullPointerException. I created a local network and see the picture to see the network information http://s9.postimg.org/k3a7t754f/image.png and when I connected to the network my IP address in the phone is 192.168.173.205

  • this is my first class UserService this class will call the

    package com.routingware.services;    
    import android.util.Log;
    
    import com.routingware.database.models.User;
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.PropertyInfo;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    
    public  String NAMESPACE = "http://tempuri.org/";
    public  String METHOD    = "";
    public  String ACTION    = "";
    public  String URL       = "http://192.168.173.1/projectnetwork/";
    
    public class UserService extends CommonClass {
    
        public UserService (String MethodName, String PageName) {
            METHOD = MethodName;
            ACTION = NAMESPACE + MethodName;
            URL    = URL + PageName;
        }
    
        public User CheckLogin (String Username, String Password) {
    
            User user = null;
    
            SoapObject Request = new SoapObject(NAMESPACE, METHOD);
    
            // Send Username as parameter
            PropertyInfo Pi = new PropertyInfo();
            Pi.setName("Username");
            Pi.setValue(Username);
            Pi.setType(String.class);
            Request.addProperty(Pi);
    
            // Send Password as parameter
            Pi = new PropertyInfo();
            Pi.setName("Password");
            Pi.setValue(Password);
            Pi.setType(String.class);
            Request.addProperty(Pi);
    
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(Request);
            envelope.implicitTypes = true;
    
            envelope.addMapping(NAMESPACE, "User",new User().getClass());
            HttpTransportSE httpTransport = new HttpTransportSE(URL);
            httpTransport.debug = true;
    
            try
            {
                Log.e("call","1");
                httpTransport.call(ACTION, envelope);
                SoapObject Response =  (SoapObject)envelope.getResponse();
    
                user = new User();
                user.setUserID(Integer.parseInt(Response.getProperty(0).toString()));
                user.setUserName(Response.getProperty(1).toString());
                user.setUserPasswrd(Response.getProperty(2).toString());
                user.setBranchID(Integer.parseInt(Response.getProperty(3).toString()));
                user.setCompanyID(Integer.parseInt(Response.getProperty(4).toString()));
                user.setUserState(Boolean.parseBoolean(Response.getProperty(5).toString()));
            }
            catch (Exception ex)
            {
                Log.e("call",ex.toString());
                return null;
            }
            finally
            {
                return user;
            }
        }
    }
    
  • call method from AsyncTaskclass

    @Override
    protected User doInBackground(Void... params)
    {
        try
        {
            return new UserService("CheckFirstTimeLogin", "UserApi.asmx")
                    .CheckLogin(mUsername, mPassword);
        }
        catch (Exception e)
        {
            return null;
        }
    }
    
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        alertDialog = new AlertDialog.Builder(LoginActivity.this).create();
    }
    
    @Override
    protected void onPostExecute(User user)
    {
        if (user == null)
        {
            onCancelled();
            alertDialog.setTitle(R.string.login_connecnt_title);
            alertDialog.setMessage("خطأ اثناء عملية الإتصال");
            alertDialog.show();
        }
    }
    

sorry for my bad english


Solution

  • The problem was that ksoap2 using the port of http which is 80 so i want to firewall and make it open to another devices i followed the steps from here http://www.thewindowsclub.com/block-open-port-windows-8-firewall