我嘗試將我的Android應用程序連接到Web服務。 我寫了一個新的類和定義的一些變量:Android Studio Webservice調用「HTTP請求失敗,HTTP狀態:401」未授權
從來就拿到了異步類使用網絡
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);
}
}
而且我有一個類,我想調用web服務
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";
}
}
}
我已經設置了使用許可權在清單文件
<uses-permission android:name="android.permission.INTERNET"/>
在我的MainActivity做我執行AsynkTask一個按鈕
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetValueTask getValueTask = new GetValueTask();
getValueTask.execute(new ApiConnector());
}
});
執行後,我獲得以下logcat的條目:
W/System.err﹕ org.ksoap2.transport.HttpResponseException: HTTP request failed, HTTP status: 401
我GOOGLE了一整天了,但我並沒有解決了這個問題。 有沒有人可以幫助我,或者可以給我提示,我必須搜索?
你有沒有使用從別的地方這些憑據得手,像瀏覽器或小提琴手/ wireshark /等? – nasch 2015-03-31 18:31:08
是的,我可以用Internet Explorer調用WS – 2015-04-02 09:41:06