尽管向清单文件添加了互联网权限,为什么我仍然收到 SocketException PermissionDenied 异常?

发布于 2024-12-08 12:40:35 字数 2117 浏览 1 评论 0原文

我想调用一个肥皂网络服务,如下所示。我向清单文件添加了互联网权限,但仍然收到异常(SocketException:权限被拒绝)。

class CallWebService extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... parameters) {
        final DefaultHttpClient httpClient=new DefaultHttpClient();
        // request parameters
        HttpParams params = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSoTimeout(params, 15000);
        // set parameter
        HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);

        // POST the envelope
        HttpPost httppost = new HttpPost(parameters[0]);
        // add headers
        httppost.setHeader("soapaction", parameters[1]);
        httppost.setHeader("Content-Type", "text/xml; charset=utf-8");

        String responseString="";
        try {

            // the entity holds the request
            HttpEntity entity = new StringEntity(parameters[2]); 
            httppost.setEntity(entity);

            // Response handler
            ResponseHandler<String> rh=new BasicResponseHandler();

            /*{
            // invoked when client receives response
            public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

             // get response entity
             HttpEntity entity = response.getEntity();

             // read the response as byte array
                   StringBuffer out = new StringBuffer();
                   byte[] b = EntityUtils.toByteArray(entity);

                   // write the response byte array to a string buffer
                   out.append(new String(b, 0, b.length));        
                   return out.toString();
            }
           };
             */
            responseString=httpClient.execute(httppost, rh); 

        } 
        catch (Exception e) {
            Log.v("exception", e.toString());
        }

        // close the connection
        httpClient.getConnectionManager().shutdown();
        return responseString;
    } 
}

I want to call a soap webservice as below. I added internet permission to manifest file but i am still getting the exception (SocketException: permission denied).

class CallWebService extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... parameters) {
        final DefaultHttpClient httpClient=new DefaultHttpClient();
        // request parameters
        HttpParams params = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSoTimeout(params, 15000);
        // set parameter
        HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);

        // POST the envelope
        HttpPost httppost = new HttpPost(parameters[0]);
        // add headers
        httppost.setHeader("soapaction", parameters[1]);
        httppost.setHeader("Content-Type", "text/xml; charset=utf-8");

        String responseString="";
        try {

            // the entity holds the request
            HttpEntity entity = new StringEntity(parameters[2]); 
            httppost.setEntity(entity);

            // Response handler
            ResponseHandler<String> rh=new BasicResponseHandler();

            /*{
            // invoked when client receives response
            public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

             // get response entity
             HttpEntity entity = response.getEntity();

             // read the response as byte array
                   StringBuffer out = new StringBuffer();
                   byte[] b = EntityUtils.toByteArray(entity);

                   // write the response byte array to a string buffer
                   out.append(new String(b, 0, b.length));        
                   return out.toString();
            }
           };
             */
            responseString=httpClient.execute(httppost, rh); 

        } 
        catch (Exception e) {
            Log.v("exception", e.toString());
        }

        // close the connection
        httpClient.getConnectionManager().shutdown();
        return responseString;
    } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月夜 2024-12-15 12:40:35

您是否将互联网权限置于清单的顶层(应用程序)(如下所示)。如果你把它放低一点,不会报告错误,但你将无法访问互联网......

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package=...
    android:versionCode="1"
    android:versionName="1.0">
    <application 
        ...
    </application>
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest> 

Have you put the internet permission at the top (application) level of your manifest (as below). No errors are reported if you put it lower down, but you will not get access to the internet...

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package=...
    android:versionCode="1"
    android:versionName="1.0">
    <application 
        ...
    </application>
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文