Android新手,我的.Net webservices驻留在我的电脑上。当我的Android应用程序使用以下代码从模拟器调用web服务时(其中XX.XX.XX.XX是我的IP地址),它运行完美。
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 1000000); //Timeout Limit
HttpResponse response;
try {
HttpPost post = new HttpPost("http://XX.XX.XX.XX/SDService_SAFTI/ServiceSD.svc/LoginUser");
但在移动设备上运行相同的代码不起作用。错误如下。这可能是港口问题吗?手机和PC都在同一个网络上。欢迎任何建议。非常感谢提前。
03-03 08:55:37.350 3273-4019/com.androidatc.customviewindrawer W/System.err: org.apache.http.conn.ConnectTimeoutException: Connect to /XX.XX.XX.XX:80 timed out
03-03 08:55:37.350 3273-4019/com.androidatc.customviewindrawer W/System.err: at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
03-03 08:55:37.350 3273-4019/com.androidatc.customviewindrawer W/System.err: at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:163)
03-03 08:55:37.350 3273-4019/com.androidatc.customviewindrawer W/System.err: at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-03 08:55:37.350 3273-4019/com.androidatc.customviewindrawer W/System.err: at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-03 08:55:37.350 3273-4019/com.androidatc.customviewindrawer W/System.err: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-03 08:55:37.350 3273-4019/com.androidatc.customviewindrawer W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:597)
03-03 08:55:37.350 3273-4019/com.androidatc.customviewindrawer W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:517)
03-03 08:55:37.350 3273-4019/com.androidatc.customviewindrawer W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:495)
03-03 08:55:37.350 3273-4019/com.androidatc.customviewindrawer W/System.err: at com.androidatc.customviewindrawer.LoginFrag$1.run(LoginFrag.java:121)
运行一段时间后,现在显示在logcat下面。
03-03 09:05:56.793 12466-12685/com.androidatc.customviewindrawer I/System.out: [CDS]connect[/192.168.0.102:80] tm:1000
03-03 09:08:09.392 12466-12517/com.androidatc.customviewindrawer I/ViewRootImpl: ANR Key Analyze: No Key event currently.
03-03 09:08:09.394 12466-12517/com.androidatc.customviewindrawer I/ViewRootImpl: ANR Key Analyze: Previeous Event null,finish at 2016-03-03 09:05:56.094
03-03 09:08:09.394 12466-12517/com.androidatc.customviewindrawer I/ViewRootImpl: ANR Motion Analyze: No motion event currently.
03-03 09:08:09.395 12466-12517/com.androidatc.customviewindrawer I/ViewRootImpl: ANR Motion Analyze: Previeous Event MotionEvent { action=ACTION_UP, id[0]=0, x[0]=609.4357, y[0]=967.4961, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=243593704, downTime=243593647, deviceId=6, source=0x1002 },finish at 2016-03-03 09:05:56.778
03-03 09:08:09.404 12466-12488/com.androidatc.customviewindrawer W/Binder: Caught a RuntimeException from the binder stub implementation.
03-03 09:08:09.404 12466-12488/com.androidatc.customviewindrawer W/Binder: java.lang.NullPointerException: Attempt to invoke interface method 'android.os.IBinder com.mediatek.anrappmanager.IFrameworks.serviceManagerGetService(java.lang.String)' on a null object reference
03-03 09:08:09.404 12466-12488/com.androidatc.customviewindrawer W/Binder: at com.mediatek.anrappmanager.ANRManagerNative$1.b(SourceFile:77)
03-03 09:08:09.404 12466-12488/com.androidatc.customviewindrawer W/Binder: at com.mediatek.anrappmanager.ANRManagerNative$1.c(SourceFile:75)
03-03 09:08:09.404 12466-12488/com.androidatc.customviewindrawer W/Binder: at com.mediatek.anrappmanager.ANRManagerNative$a.get(SourceFile:97)
03-03 09:08:09.404 12466-12488/com.androidatc.customviewindrawer W/Binder: at com.mediatek.anrappmanager.ANRManagerNative.getDefault(SourceFile:35)
03-03 09:08:09.404 12466-12488/com.androidatc.customviewindrawer W/Binder: at com.mediatek.anrappmanager.ANRAppManager.dumpMessageHistory(SourceFile:59)
03-03 09:08:09.404 12466-12488/com.androidatc.customviewindrawer W/Binder: at android.app.ActivityThread$ApplicationThread.dumpMessageHistory(ActivityThread.java:1232)
03-03 09:08:09.404 12466-12488/com.androidatc.customviewindrawer W/Binder: at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:679)
03-03 09:08:09.404 12466-12488/com.androidatc.customviewindrawer W/Binder: at android.os.Binder.execTransact(Binder.java:451)
03-03 09:08:09.405 12466-12488/com.androidatc.customviewindrawer W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'android.os.IBinder com.mediatek.anrappmanager.IFrameworks.serviceManagerGetService(java.lang.String)' on a null object reference
03-03 09:08:09.405 12466-12488/com.androidatc.customviewindrawer W/System.err: at com.mediatek.anrappmanager.ANRManagerNative$1.b(SourceFile:77)
03-03 09:08:09.405 12466-12488/com.androidatc.customviewindrawer W/System.err: at com.mediatek.anrappmanager.ANRManagerNative$1.c(SourceFile:75)
03-03 09:08:09.405 12466-12488/com.androidatc.customviewindrawer W/System.err: at com.mediatek.anrappmanager.ANRManagerNative$a.get(SourceFile:97)
03-03 09:08:09.405 12466-12488/com.androidatc.customviewindrawer W/System.err: at com.mediatek.anrappmanager.ANRManagerNative.getDefault(SourceFile:35)
03-03 09:08:09.405 12466-12488/com.androidatc.customviewindrawer W/System.err: at com.mediatek.anrappmanager.ANRAppManager.dumpMessageHistory(SourceFile:59)
03-03 09:08:09.405 12466-12488/com.androidatc.customviewindrawer W/System.err: at android.app.ActivityThread$ApplicationThread.dumpMessageHistory(ActivityThread.java:1232)
03-03 09:08:09.405 12466-12488/com.androidatc.customviewindrawer W/System.err: at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:679)
03-03 09:08:09.405 12466-12488/com.androidatc.customviewindrawer W/System.err: at android.os.Binder.execTransact(Binder.java:451)
03-03 09:08:09.453 12466-12480/com.androidatc.customviewindrawer I/art: Thread[9,tid=12480,WaitingInMainSignalCatcherLoop,Thread*=0x55a71953d0,peer=0x12c53080,"Signal Catcher"]: reacting to signal 3
03-03 09:08:09.661 12466-12480/com.androidatc.customviewindrawer I/art: Wrote stack traces to '/data/anr/traces.txt'
更新 - 屏幕截图 - 端口转发:
答案 0 :(得分:1)
我这样做的方法是使用端口转发:
1)在移动设备和笔记本电脑浏览器上连接设备并打开Chrome。
2)在“URL”选项卡上,键入chrome:// inspect
3)单击端口转发并添加一个新行,端口为8888,IP为webservice_url:port。
4)现在在你的android应用程序代码中,用localhost:8888替换webservice_url:port。 基本上现在8888你的手机将调用计算机webservice网址。注意:确保您的笔记本电脑和设备上都打开了Chrome。当你输入chrome:// inspect时,你应该能够看到你附加的设备。
有关更多帮助,请参阅此处:https://developer.chrome.com/devtools/docs/remote-debugging
同时将<uses-permission android:name="android.permission.INTERNET"/>
添加到Androidmanifest.xml