通过网络访问Web服务

时间:2014-03-07 16:06:14

标签: java android android-ksoap2

我创建了一个asp.net Web服务并使用模拟器我可以轻松访问我现在想要通过网络访问Web服务的Web服务所以我的手机将是客户端而我的笔记本电脑将是服务器试图这么做几个小时但没有发生任何事情仍然给了我java.lang.NullPointerException。 我创建了一个本地网络并查看图片以查看网络信息http://s9.postimg.org/k3a7t754f/image.png,当我连接到网络时,手机中的IP地址为192.168.173.205

  • 这是我的第一个类UserService,这个类将调用

    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;
            }
        }
    }
    
  • 来自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();
        }
    }
    
抱歉我的英文不好

2 个答案:

答案 0 :(得分:0)

你可以发布日志吗?您的设备是否连接到笔记本电脑连接的同一个wifi网络。 如果您使用本地网络作为服务,则设备应连接到同一个wifi网络。

答案 1 :(得分:0)

问题是ksoap2使用http的端口是80,所以我想要防火墙并让它对其他设备开放我按照这里的步骤http://www.thewindowsclub.com/block-open-port-windows-8-firewall