2013-02-09 183 views
0

我創建了一個小應用程序,並通過此應用程序我想將數據(通過wifi或藍牙)發送到PC /服務器。如何連接Android應用程序連接到Web服務

  1. 我正在考慮創建一個可以在PC上運行的web服務,並且會不斷收聽任何傳入的客戶端請求。

  2. 一旦它從客戶端收到請求,就會發生數據傳輸。在webservice收到數據後,它應該自動打開一個顯示接收數據的應用程序/ GUI窗口。

我的問題是我可以在JAVA中使用TCP/IP創建一個web服務,並讓它不斷在後臺運行並監聽客戶端請求? 如何在webservice檢測到客戶端請求並接收數據後立即啓動GUI?

+0

是的,你可以做到這一點的方式。但是當你連續呼叫web服務時,你的應用程序的性能變得非常低。 – Kels 2013-02-09 15:14:54

回答

1

我建議在服務器端使用WS,並且可以在客戶端使用Ksoap android library。我個人的opninios是你認爲正確的:)創建一個後臺線程,用新的數據更新UI。使用適合您的解決方案的AsyncTask,Loader或Handler。 (請閱讀more info的官方參考資料)。

我希望它能幫你!

1

使用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

1

最好的辦法是創建一個Web服務和應用程序連接到Web服務,您可以使用創建用於連接和分析數據所需的全部代碼的工具http://www.wsdl2code.com :)

1

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(); 
    } 
} 

} 
相關問題