gpt4 book ai didi

java - Android:尝试 HttpURLConnection.getOutputStream() 时抛出 SocketException

转载 作者:搜寻专家 更新时间:2023-11-01 08:15:41 29 4
gpt4 key购买 nike

这是我的第一个 android 程序,它是一个修改过的 hello world。我在 MAC 上的模拟器上运行它。我尝试与 .NET Web 服务通信,但它在 connection.getOutputStream() 处出现异常。我可以从模拟器访问该站点。任何帮助表示赞赏:)


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)

最佳答案

尝试在您的 AndroidManifest.xml 中添加“internet”权限:

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

关于java - Android:尝试 HttpURLConnection.getOutputStream() 时抛出 SocketException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5168494/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com