如何连接Android应用程序连接到Web服务
我创建了一个小应用程序,并通过此应用程序我想将数据(通过wifi或蓝牙)发送到PC /服务器。如何连接Android应用程序连接到Web服务
我正在考虑创建一个可以在PC上运行的web服务,并且会不断收听任何传入的客户端请求。
一旦它从客户端收到请求,就会发生数据传输。在webservice收到数据后,它应该自动打开一个显示接收数据的应用程序/ GUI窗口。
我的问题是我可以在JAVA中使用TCP/IP创建一个web服务,并让它不断在后台运行并监听客户端请求? 如何在webservice检测到客户端请求并接收数据后立即启动GUI?
我建议在服务器端使用WS,并且可以在客户端使用Ksoap android library。我个人的opninios是你认为正确的:)创建一个后台线程,用新的数据更新UI。使用适合您的解决方案的AsyncTask,Loader或Handler。 (请阅读more info的官方参考资料)。
我希望它能帮你!
使用SQL Server管理桌面上的数据并在Visual Studio中使用.NET创建Web服务。
然后连接到应用程序中的Web服务,并使用Web服务设置/从数据库获取数据。
链接,这可能是有用的:
如何使.NET中的Web服务(不包括在Android上实现):http://srikanthtechnologies.com/blog/dotnet/wsdaljava.aspx
如何对您的服务与连接安卓:
http://seesharpgears.blogspot.in/2010/11/basic-ksoap-android-tutorial.html
http://www.codeproject.com/Articles/304302/Calling-Asp-Net-Webservice-ASMX-From-an-Android-Ap
http://adrianandroid.blogspot.in/2012/05/access-c-net-web-service-in.html
http://sidharthkumargth.github.io/UserConnection
或
https://github.com/sidharthkumargth/UserConnection
下载此userconnection.jar从上面的链接,并导入到库
例:
public class MainActivity extends Activity implements ConnectionStatusListener
{
UserConnection uc,uc1;
ImageView iv;
TextView tv;
ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setSubtitle("Only for test");
iv=(ImageView)findViewById(R.id.imageView1);
tv=(TextView)findViewById(R.id.ttt);
pb=(ProgressBar)findViewById(R.id.progressBar1);
uc=new UserConnection(2); // here 2 is number of objects sent to web service email and password, if the number of objects is n then it will be uc=new UserConnection(n);
uc.setHTTPMethod("POST");
uc.setupValues("email", "[email protected]");
uc.setupValues("password", "test");
uc.setConnectionStatusListener(this);
//uc.startConnection("http://test.com/tb/login.php");
//uc.startDownloadfrom("http://www.kingsoftstore.com/images/presentation-templates/report-ppt-template-059.dpt","/sdcard/downloadedfile.jpg");
//uc.startDownloadImage("http://i00.i.aliimg.com/wsphoto/v0/1157302568/2013-girls-new-style-popular-purple-sexy-lingeries-LC1105-free-shipping.jpg");
uc.startDownloadfrom("http://www.technotrigger.com/wp-content/uploads/2014/01/house-in-green-field.jpg","/sdcard/downloadedfile1.jpg");
uc.startDownloadfrom("http://upload.wikimedia.org/wikipedia/commons/3/36/Hopetoun_falls.jpg","/sdcard/downloadedfile2.jpg");
uc.startDownloadfrom("http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg","/sdcard/downloadedfile3.jpg");
uc.startDownloadfrom("http://tmacfitness.com/wp-content/uploads/2013/04/Beauty-of-nature-random-4884759-1280-800.jpg","/sdcard/downloadedfile4.jpg");
uc.startDownloadfrom("http://freedwallpaper.com/wp-content/uploads/2014/03/6-autumn-nature-sandbox-images_2053316.jpg","/sdcard/downloadedfile5.jpg");
//uc.startMultipartConnection("http://www.google.co.in");
uc1=new UserConnection(0);
uc1.setHTTPMethod("GET");
//"/sdcard/downloadedfile.jpg"
uc1.setConnectionStatusListener(this);
uc1.startConnection("http://www.facebook.com");
}
//Toast.makeText(getApplicationContext(), message, 1).show();
@Override
public void onError(Status arg0) {
// TODO Auto-generated method stub
try {
Toast.makeText(getApplicationContext(), "error"+arg0.getErrorMessage(), 1).show();
} catch (UserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onStart(Status arg0) {
// TODO Auto-generated method stub
try{
if(arg0.getConnectionHolder()==uc){
Toast.makeText(getApplicationContext(), "uc"+arg0.getMessage(), 1).show();
}else if(arg0.getConnectionHolder()==uc1){
Toast.makeText(getApplicationContext(), "uc1"+arg0.getMessage(), 1).show();
}
}catch(UserException e){}
}
@Override
public void onStop(Status arg0) {
// TODO Auto-generated method stub
//Log.d(arg0.getConnectionHolder().toString(), arg0.getResponse()+"");
try {
iv.setImageDrawable(Drawable.createFromPath(arg0.getFile()));
//Toast.makeText(getApplicationContext(), arg0.getConnectionHolder()+"++"+arg0.getResponse(), Toast.LENGTH_LONG).show();
} catch (UserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onProgressUpdate(Status arg0) {
// TODO Auto-generated method stub
try {
tv.setText("Progress "+arg0.getProgress());
pb.setProgress(arg0.getProgress());
} catch (UserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
是的,你可以做到这一点的方式。但是当你连续呼叫web服务时,你的应用程序的性能变得非常低。 – Kels 2013-02-09 15:14:54