简体   繁体   English

通过网络访问Web服务

[英]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 . 我创建了一个asp.net Web服务,并使用仿真器,现在我想通过网络访问Web服务,因此可以轻松地访问该Web服务,因此我的手机将成为客户端,而笔记本电脑将成为服务器。那花了好几个小时但什么都没发生仍然给了我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 我创建了一个本地网络,并看到图片以查看网络信息http://s9.postimg.org/k3a7t754f/image.png ,当我连接到网络时,手机中的IP地址为192.168.173.205

  • this is my first class UserService this class will call the 这是我的第一个类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; } } } 
  • call method from AsyncTaskclass AsyncTaskclass的call方法

     @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 对不起,我的英语不好

Can you post the logs?. 您可以发布日志吗? Is your device connected to the same wifi network your laptop is connected. 您的设备是否连接到与笔记本电脑相同的wifi网络。 Snce you are using local network as your service, the device should be connected to the same wifi network. 如果您使用本地网络作为服务,则设备应连接到相同的wifi网络。

问题是ksoap2使用http的端口为80,所以我要防火墙并使其对其他设备开放,我按照此处的步骤操作http://www.thewindowsclub.com/block-open-port-windows-8 -防火墙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM