如何从android中的dotnet web服务获取数据?
我不熟悉web服务。我已经将数据保存在web服务中,但我不知道如何从web服务获取这些数据。我在.net中的web服务在该web服务中使用了SOAP解析。所以,请帮助我如何从url获取数据。我在谷歌搜索过,但没有找到解决方案 我已使用以下代码将数据保存到webservice中如何从android中的dotnet web服务获取数据?,android,.net,web-services,Android,.net,Web Services,我不熟悉web服务。我已经将数据保存在web服务中,但我不知道如何从web服务获取这些数据。我在.net中的web服务在该web服务中使用了SOAP解析。所以,请帮助我如何从url获取数据。我在谷歌搜索过,但没有找到解决方案 我已使用以下代码将数据保存到webservice中 package com.soap; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2
package com.soap;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.*;
import android.os.*;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyworldActivity extends Activity {
EditText name,uname,pass,num,mail,img;
Button save,back;
private static final String SOAP_ACTION = "http://localhost/service1/InsertUsertRegistrationDetails";
private static final String METHOD_NAME = "InsertUsertRegistrationDetails";
private static final String NAMESPACE = "http://localhost/service1";
private static final String URL = "http://113.193.181.53/MyWorldApp/Service1.asmx";
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.text1);
name = (EditText)findViewById(R.id.name);
uname = (EditText)findViewById(R.id.uname);
pass = (EditText)findViewById(R.id.pass);
num = (EditText)findViewById(R.id.num);
mail = (EditText)findViewById(R.id.mail);
img = (EditText)findViewById(R.id.img);
save = (Button)findViewById(R.id.save);
back = (Button)findViewById(R.id.back);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
call();
}
});
}
public void call()
{
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Name", name.getText().toString());
request.addProperty("UserName", uname.getText().toString());
request.addProperty("Password", pass.getText().toString());
request.addProperty("MobileNumber", num.getText().toString());
request.addProperty("EmailID", mail.getText().toString());
request.addProperty("image",img.getText().toString());
Log.e("success","success");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
tv.setText(result.toString());
} catch (Exception e) {
tv.setText(e.getMessage());
}
}
}
谁能告诉我如何从dotnetweb服务获取数据
提前谢谢 您可以使用HTTP请求/响应来代替SOAP请求 请参见以下示例代码:
/**
* Connects to server. Sends data to server. Receive data from server.
*
* @param request
* IN parameter. Request string.
* @param response
* OUT parameter, to get data which receives from server.
* @param error
* OUT parameter, to get error information.
* @return - Function status, true or false.
*/
private boolean requestData(URI uri, StringBuilder responseString, StringBuilder error) {
HttpResponse response = null;
boolean isSuccess = true;
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT);
HttpGet request = new HttpGet(uri);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
response = httpClient.execute(request);
} catch (ClientProtocolException e) {
error.append(e.getMessage());
Log.e(Constants.LOG_TAG, "httpClient.execute() ClientProtocolException: " + e.getMessage());
e.printStackTrace();
isSuccess = false;
} catch (IOException e) {
error.append(e.getMessage());
Log.e(Constants.LOG_TAG, "httpClient.execute() IOException: " + e.getMessage());
e.printStackTrace();
// Server connection is not OK.
TrendDataMgr.getInstance().setConnectionStatus(false);
isSuccess = false;
} catch (Exception e) {
error.append(e.getMessage());
Log.e(Constants.LOG_TAG, "httpClient.execute() Exception: " + e.getMessage());
e.printStackTrace();
isSuccess = false;
}
// If response not needed, Exit.
if (null == responseString) {
return isSuccess;
}
if (response == null) {
Log.e(Constants.LOG_TAG, "requestData().httpClient.execute() failed. Response = null");
return false;
}
// Server connection is OK.
try {
HttpEntity responseEntity = response.getEntity();
String str = EntityUtils.toString(responseEntity);
responseString.append(str);
//Log.d(Constants.LOG_TAG, "httpClient.execute()" + str);
}catch(Exception e) {
error.append(e.getMessage());
Log.e(Constants.LOG_TAG, "EntityUtils.toString(), Exception: " + e.getMessage());
e.printStackTrace();
isSuccess = false;
}
return isSuccess;
}
您可以使用URL调用此函数:
String request = URL + "/" + URLEncoder.encode(parameter, "UTF-8").replace("+", "%20");
boolean isSuccess = true;
URI uri = new URI(request);
isSuccess = requestData(uri, response, error);
希望这能帮助你。天哪。我从哪里开始?首先,我会取下你(非常)公开的网站,在当地工作,直到你真正有了一些完整的东西。其次,从代码上看,你似乎在通过一个未加密的链接传递姓名、手机号码、密码和电子邮件。如果你不知道为什么你不应该这样做,那就去学习(或者找一份不同的工作)。另外,千万不要在UI线程上打网络电话。