开发者

Android: SocketException thrown when attempting HttpURLConnection.getOutputStream()

开发者 https://www.devze.com 2023-02-14 21:07 出处:网络
this is my very first android program, it\'s a modified hello world. I\'m running it on the emulator on a MAC. I attempt to communicate with a .NET web service but it gives an exception at connection.

this is my very first android program, it's a modified hello world. I'm running it on the emulator on a MAC. I attempt to communicate with a .NET web service but it gives an exception at connection.getOutputStream(). I can access the site from the emulator. Any help is appreciated :)


    URL url = new URL("http://192.168.3.47/service.asmx");
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", 
     "application/soap+xml; charset=utf-8");

    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    String soapRequest = String.format(getText(R.string.ws_listemain_ds_new).toString(), city, keyword);
    connection.setRequestProperty("Content-Length", Integer.toString(soapRequest.getBytes("UTF-8").length));
    //Send request
    OutputStreamWriter owr = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

03-02 15:51:26.950: WARN/System.err(618): java.net.SocketException: Permission denied
03-02 15:51:26.978: WARN/System.err(618):     at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
03-02 15:51:26.988: WARN/System.err(618):     at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
03-02 15:51:27.009: WARN/System.err(618):     at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
03-02 15:51:27.018: WARN/System.err(618):     at java.net.Socket.checkOpenAndCreate(Socket.java:802)
03-02 15:51:27.037: WARN/System.err(618):     at java.net.Socket.connect(Socket.java:948)
03-02 15:51:27.048: WARN/System.err(618):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:75)
03-02 15:51:27.067: WARN/System.err(618):     at org.apache.harmony.luni.inte开发者_如何学Gornal.net.www.protocol.http.HttpConnection.(HttpConnection.java:48)
03-02 15:51:27.079: WARN/System.err(618):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
03-02 15:51:27.139: WARN/System.err(618):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
03-02 15:51:27.158: WARN/System.err(618):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
03-02 15:51:27.167: WARN/System.err(618):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
03-02 15:51:27.187: WARN/System.err(618):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
03-02 15:51:27.197: WARN/System.err(618):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)
03-02 15:51:27.217: WARN/System.err(618):     at gyozo.HelloWorld.HelloActivity.onClick(HelloActivity.java:55)
03-02 15:51:27.269: WARN/System.err(618):     at android.view.View.performClick(View.java:2485)
03-02 15:51:27.311: WARN/System.err(618):     at android.view.View$PerformClick.run(View.java:9080)
03-02 15:51:27.327: WARN/System.err(618):     at android.os.Handler.handleCallback(Handler.java:587)
03-02 15:51:27.347: WARN/System.err(618):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 15:51:27.367: WARN/System.err(618):     at android.os.Looper.loop(Looper.java:123)
03-02 15:51:27.447: WARN/System.err(618):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-02 15:51:27.457: WARN/System.err(618):     at java.lang.reflect.Method.invokeNative(Native Method)
03-02 15:51:27.519: WARN/System.err(618):     at java.lang.reflect.Method.invoke(Method.java:507)
03-02 15:51:27.527: WARN/System.err(618):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-02 15:51:27.547: WARN/System.err(618):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-02 15:51:27.547: WARN/System.err(618):     at dalvik.system.NativeStart.main(Native Method)


Try to add the "internet" permission on your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
0

精彩评论

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