本文介绍了安卓:SocketException尝试HttpURLConnection.getOutputStream时,抛出()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的第一个Android程序,这是一个修改后的Hello World。我在上一个MAC模拟器中运行它。我试图与.NET的Web服务进行通信,但是它在给出了一个例外connection.getOutputStream()
。我可以从仿真器访问该站点。任何帮助AP preciated:)
网址URL =新的URL(http://192.168.3.47/service.asmx);
HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
connection.setRequestMethod(POST);
connection.setRequestProperty(内容类型,
应用程序/肥皂+ XML;字符集= UTF-8);
connection.setUseCaches(假);
connection.setDoInput(真正的);
connection.setDoOutput(真正的);
字符串SOA prequest =的String.Format(的getText(R.string.ws_listemain_ds_new)的ToString(),市,关键词);
connection.setRequestProperty(内容长度,Integer.toString(SOA prequest.getBytes(UTF-8)的长度)。);
//发送请求
OutputStreamWriter OWR =新OutputStreamWriter(connection.getOutputStream(),UTF-8);
03-02 15:51:26.950:WARN / System.err的(618):java.net.SocketException异常:权限被拒绝
03-02 15:51:26.978:WARN / System.err的(618):在org.apache.harmony.luni.platform.OSNetworkSystem.socket(本机方法)
03-02 15:51:26.988:WARN / System.err的(618):在dalvik.system.BlockGuard $ WrappedNetworkSystem.socket(BlockGuard.java:335)
03-02 15:51:27.009:WARN / System.err的(618):在org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
03-02 15:51:27.018:WARN / System.err的(618):在java.net.Socket.checkOpenAndCreate(Socket.java:802)
03-02 15:51:27.037:WARN / System.err的(618):在java.net.Socket.connect(Socket.java:948)
03-02 15:51:27.048:WARN / System.err的(618):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection(HttpConnection.java:75)
03-02 15:51:27.067:WARN / System.err的(618):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection(HttpConnection.java:48)
03-02 15:51:27.079:WARN / System.err的(618):在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):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
03-02 15:51:27.158:WARN / System.err的(618):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
03-02 15:51:27.167:WARN / System.err的(618):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
03-02 15:51:27.187:WARN / System.err的(618):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
03-02 15:51:27.197:WARN / System.err的(618):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)
03-02 15:51:27.217:WARN / System.err的(618):在gyozo.HelloWorld.HelloActivity.onClick(HelloActivity.java:55)
03-02 15:51:27.269:WARN / System.err的(618):在android.view.View.performClick(View.java:2485)
03-02 15:51:27.311:WARN / System.err的(618):在android.view.View $ PerformClick.run(View.java:9080)
03-02 15:51:27.327:WARN / System.err的(618):在android.os.Handler.handleCallback(Handler.java:587)
03-02 15:51:27.347:WARN / System.err的(618):在android.os.Handler.dispatchMessage(Handler.java:92)
03-02 15:51:27.367:WARN / System.err的(618):在android.os.Looper.loop(Looper.java:123)
03-02 15:51:27.447:WARN / System.err的(618):在android.app.ActivityThread.main(ActivityThread.java:3683)
03-02 15:51:27.457:WARN / System.err的(618):在java.lang.reflect.Method.invokeNative(本机方法)
03-02 15:51:27.519:WARN / System.err的(618):在java.lang.reflect.Method.invoke(Method.java:507)
03-02 15:51:27.527:WARN / System.err的(618):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
03-02 15:51:27.547:WARN / System.err的(618):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-02 15:51:27.547:WARN / System.err的(618):在dalvik.system.NativeStart.main(本机方法)
解决方案
尝试添加了互联网权限您的AndroidManifest.xml:
<使用-权限的Android:名称=android.permission.INTERNET对/>
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.internal.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" />
这篇关于安卓:SocketException尝试HttpURLConnection.getOutputStream时,抛出()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!