I am facing some problems in Android as I am new to it. I need to know how to write syntax in Android for the below problem.
我在Android中面临一些问题,因为我是新手。我需要知道如何在Android中编写语法来解决以下问题。
Problem: I have a .net web service (www.somesite.com
). That webserver has an authentication method which requires a user name and a password as a parameter to authenticate. Once I set those things using the authentication method, it will allow me to call the rest of the functionality present in the webserver. I have the source code written in ASP. I want to write the same code in Android.
问题:我有.net网络服务(www.somesite.com)。该Web服务器具有一种身份验证方法,该方法需要用户名和密码作为参数进行身份验证。一旦我使用身份验证方法设置了这些内容,它将允许我调用Web服务器中存在的其余功能。我有用ASP编写的源代码。我想在Android中编写相同的代码。
private MyServerAPI.Service _service;
_service = new MyServerAPI.Service();
MyServerAPI.DTAuthHeader auth = new MyServerAPI.DTAuthHeader();
auth.Username = ConfigurationManager.AppSettings["MyServerAPI.user"];
auth.Password = ConfigurationManager.AppSettings["MyServerAPI.pass"];
_service.DTAuthHeaderValue = auth;
_service.Url = ConfigurationManager.AppSettings["MyServerAPI.service"];
Basically, I want to write the same thing as the above code in Android.
基本上,我想在Android中编写与上述代码相同的内容。
2 个解决方案
#1
0
There's a library for dealing with WebServices called KSOAP2
. find it here
有一个用于处理名为KSOAP2的WebServices的库。在这里找到它
Use it and notice how your server requrires the username and password to be sent.
使用它并注意您的服务器如何要求发送用户名和密码。
UPDATE
Code Snippet:
更新代码片段:
SoapObject soapRequest = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME) ;
soapRequest.addProperty("appName","MyCoolApp" );
soapRequest.addProperty("sEmail","test@example.com" );
soapRequest.addProperty("sPassword","test" );
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(soapRequest);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try{
httpTransport.call(SOAP_ACTION, soapEnvelope);
Object response = soapEnvelope.getResponse();
Log.d("-----RESPONSE",""+response);
}catch (Exception exception){
Log.d("RESPONSE",""+exception.toString());
}
Although I discourage posting whole code here, but since you seem to be in dire trouble, I hope this much helps you.
虽然我不鼓励在这里发布整个代码,但由于你似乎遇到了麻烦,我希望这对你有所帮助。
Or wait a bit I'll blog about it, in detail.
或者等一下我会在博客上详细介绍它。
#2
0
If you dont want to depend on 3rd party library then you call also use the inbuild class of android itself.
如果您不想依赖第三方库,那么您也可以使用android本身的inbuild类。
Here is an function which is used to post data on your webserver along with passing username and password.
这是一个用于在您的网络服务器上发布数据以及传递用户名和密码的功能。
public void postDataToWeb() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.yoursite.com");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Username", "Paresh"));
nameValuePairs.add(new BasicNameValuePair("Password", "PM@Android"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Now, you are having response in inputstream (in above example, 'is') and you can play with inputstream.
现在,您在输入流中有响应(在上面的例子中,'是'),您可以使用输入流。
#1
0
There's a library for dealing with WebServices called KSOAP2
. find it here
有一个用于处理名为KSOAP2的WebServices的库。在这里找到它
Use it and notice how your server requrires the username and password to be sent.
使用它并注意您的服务器如何要求发送用户名和密码。
UPDATE
Code Snippet:
更新代码片段:
SoapObject soapRequest = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME) ;
soapRequest.addProperty("appName","MyCoolApp" );
soapRequest.addProperty("sEmail","test@example.com" );
soapRequest.addProperty("sPassword","test" );
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(soapRequest);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try{
httpTransport.call(SOAP_ACTION, soapEnvelope);
Object response = soapEnvelope.getResponse();
Log.d("-----RESPONSE",""+response);
}catch (Exception exception){
Log.d("RESPONSE",""+exception.toString());
}
Although I discourage posting whole code here, but since you seem to be in dire trouble, I hope this much helps you.
虽然我不鼓励在这里发布整个代码,但由于你似乎遇到了麻烦,我希望这对你有所帮助。
Or wait a bit I'll blog about it, in detail.
或者等一下我会在博客上详细介绍它。
#2
0
If you dont want to depend on 3rd party library then you call also use the inbuild class of android itself.
如果您不想依赖第三方库,那么您也可以使用android本身的inbuild类。
Here is an function which is used to post data on your webserver along with passing username and password.
这是一个用于在您的网络服务器上发布数据以及传递用户名和密码的功能。
public void postDataToWeb() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.yoursite.com");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Username", "Paresh"));
nameValuePairs.add(new BasicNameValuePair("Password", "PM@Android"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Now, you are having response in inputstream (in above example, 'is') and you can play with inputstream.
现在,您在输入流中有响应(在上面的例子中,'是'),您可以使用输入流。