Android Studio Webservice调用“HTTP请求失败,HTTP状态:401”未经授权

时间:2022-09-23 08:17:43

i try to connect my Android application to a Webservice. I wrote a new class and defined some Variables:

我尝试将我的Android应用程序连接到Web服务。我写了一个新类并定义了一些变量:

I´ve got the Async Class to use the Network

我有Async Class来使用网络

class GetValueTask extends AsyncTask<ApiConnector,Long,String> {
    @Override
    protected String doInBackground(ApiConnector... params) {
        //wird im Background Thread ausgeführt
        return  params[0].getValue();

    }

    @Override
    protected void onPostExecute(String s) {
        //wird im Mainthread ausgeführt
        MainActivity.this.setText(s);
    }
}

And I have a Class where i want to call the Webservice

我有一个类,我想称之为Webservice

public class ApiConnector
{
    private static final String SOAP_ACTION ="urn:microsoft-dynamics-schemas/codeunit/AddService:Add";
    private static final String METHOD_NAME ="Add";
    private static final String NAMESPACE ="urn:microsoft-dynamics-schemas/codeunit/AddService";
    private static final String URL ="http://192.168.0.154:9047/DynamicsNAV80/WS/CRONUS%20AG/Codeunit/AddService";
    private static final String USERNAME="B.Denger";
    private static final String PASSWORD ="TestPW123!";



    public String getValue() {

        SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
        request.addProperty("no","10");

        PropertyInfo unamePI = new PropertyInfo();
        PropertyInfo passPI = new PropertyInfo();
        // Set Username
        unamePI.setName("username");
        // Set Value
        unamePI.setValue(USERNAME);
        // Set dataType
        unamePI.setType(String.class);
        // Add the property to request object
        request.addProperty(unamePI);
        //Set Password
        passPI.setName("password");
        //Set dataType
        passPI.setValue(PASSWORD);
        //Set dataType
        passPI.setType(String.class);
        //Add the property to request object
        request.addProperty(passPI);

        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        soapEnvelope.setOutputSoapObject(request);

        HttpTransportSE aht= new HttpTransportSE(URL);

        try {
            aht.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
            return resultString.toString();
        }catch(Exception e ) {
            e.printStackTrace();
            return "Fail at Call";
        }
    }
}

I have set the using-permission in the Manifest file

我在Manifest文件中设置了使用权限

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

in my MainActivity do i execute the AsynkTask with a Button

在我的MainActivity中,我用Button执行AsynkTask

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GetValueTask getValueTask = new GetValueTask();
            getValueTask.execute(new ApiConnector());
        }
    });

after execution i get following Logcat entry:

执行后我得到以下Logcat条目:

W/System.err﹕ org.ksoap2.transport.HttpResponseException: HTTP request failed, HTTP status: 401

i googled a whole Day for it, but i did not solved the problem yet. is there anybody who could help me, or could give me a hint where i have to search?

我用Google搜索了一整天,但我还没有解决问题。有没有人可以帮助我,或者可以给我一个提示,我必须搜索?

2 个解决方案

#1


0  

I found the solution here:

我在这里找到了解决方案:

Android Consuming Dynamics NAV SOAP Web Service

Android消费动态NAV SOAP Web服务

but it didnt Work with the jcif 1.3.17 jar at https://jcifs.samba.org/src/ can you download the latest version.

但它没有使用https://jcifs.samba.org/src/上的jcif 1.3.17 jar,你可以下载最新版本。

#2


0  

In my case I fixed same problem by adding this code:

在我的情况下,我通过添加此代码修复了同样的问题:

List<HeaderProperty> llstHeadersProperty = new ArrayList<>();
llstHeadersProperty.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("user:password".getBytes())));
loHttpTransport.call(sSOAP_ACTION, loEnvelope, llstHeadersProperty);

Complete task:

private class fnAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params)
        {
            //for linear parameter
            SoapObject loRequest = new SoapObject(sNAMESPACE, sMETHOD_NAME);
            // adding method property here serially
//            loRequest.addProperty("CountryName", "france");

            SoapSerializationEnvelope loEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            loEnvelope.implicitTypes = true;
            loEnvelope.setOutputSoapObject(loRequest);
            loEnvelope.dotNet = true;


            HttpTransportSE loHttpTransport = new HttpTransportSE(_sURL);
            loHttpTransport.debug = true;
            try
            {
                List<HeaderProperty> llstHeadersProperty = new ArrayList<>();
                llstHeadersProperty.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("user:password".getBytes())));
                loHttpTransport.call(sSOAP_ACTION, loEnvelope, llstHeadersProperty);
            }
            catch (HttpResponseException e)
            {
                // TODO Auto-generated catch block
                Log.e("HTTPLOG", e.getMessage());
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e("IOLOG", e.getMessage());
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                Log.e("XMLLOG", e.getMessage());
                e.printStackTrace();
            } //send request

            Object result = null;
            try {
                result = (Object )loEnvelope.getResponse();
                //See output in the console
                Log.i("RESPONSE",String.valueOf(result));
            } catch (SoapFault e) {
                // TODO Auto-generated catch block
                Log.e("SOAPLOG", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
    }

Full example http://www.nascenia.com/consuming-soap-web-services-from-android/

完整示例http://www.nascenia.com/consuming-soap-web-services-from-android/

#1


0  

I found the solution here:

我在这里找到了解决方案:

Android Consuming Dynamics NAV SOAP Web Service

Android消费动态NAV SOAP Web服务

but it didnt Work with the jcif 1.3.17 jar at https://jcifs.samba.org/src/ can you download the latest version.

但它没有使用https://jcifs.samba.org/src/上的jcif 1.3.17 jar,你可以下载最新版本。

#2


0  

In my case I fixed same problem by adding this code:

在我的情况下,我通过添加此代码修复了同样的问题:

List<HeaderProperty> llstHeadersProperty = new ArrayList<>();
llstHeadersProperty.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("user:password".getBytes())));
loHttpTransport.call(sSOAP_ACTION, loEnvelope, llstHeadersProperty);

Complete task:

private class fnAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params)
        {
            //for linear parameter
            SoapObject loRequest = new SoapObject(sNAMESPACE, sMETHOD_NAME);
            // adding method property here serially
//            loRequest.addProperty("CountryName", "france");

            SoapSerializationEnvelope loEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            loEnvelope.implicitTypes = true;
            loEnvelope.setOutputSoapObject(loRequest);
            loEnvelope.dotNet = true;


            HttpTransportSE loHttpTransport = new HttpTransportSE(_sURL);
            loHttpTransport.debug = true;
            try
            {
                List<HeaderProperty> llstHeadersProperty = new ArrayList<>();
                llstHeadersProperty.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("user:password".getBytes())));
                loHttpTransport.call(sSOAP_ACTION, loEnvelope, llstHeadersProperty);
            }
            catch (HttpResponseException e)
            {
                // TODO Auto-generated catch block
                Log.e("HTTPLOG", e.getMessage());
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e("IOLOG", e.getMessage());
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                Log.e("XMLLOG", e.getMessage());
                e.printStackTrace();
            } //send request

            Object result = null;
            try {
                result = (Object )loEnvelope.getResponse();
                //See output in the console
                Log.i("RESPONSE",String.valueOf(result));
            } catch (SoapFault e) {
                // TODO Auto-generated catch block
                Log.e("SOAPLOG", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
    }

Full example http://www.nascenia.com/consuming-soap-web-services-from-android/

完整示例http://www.nascenia.com/consuming-soap-web-services-from-android/