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;
    } 
}
4

1 回答 1

2

您是否将互联网权限放在清单的顶部(应用程序)级别(如下所示)。如果您将其放低,则不会报告错误,但是您将无法访问互联网...

<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> 
于 2011-10-05T21:51:26.227 回答