0

Dont get angry on me please. I have two questions, I think on very similar theme, so I decided to merge them into one. I have my app on android that is using sensors to do some calculations. I am storing there sesults in my database. What i want to do is to send my data from my phone to my desktop app also with a database (on button click).

To be more precise, here is an example: My light sensor reads current light intensity. Lets say it is 1000lux. Now, when I click my button "Send" in my android app, it will send this value to my desktop apps database. That desktop app will read that value and will show it to user.

Is it possible via WIFI? or better via web, so i will not be limited with distance? How can android manage this kind of communication?

And my second question is, if controlling media player on my PC is similar to what i said.

EDIT:

I did some research and found one Socket tutorial. I tried it exactly like it is there. So i have this in my android app:

public class Client extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client);
    Client myCli = new Client();
    try {
        myCli.run();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_client, menu);
    return true;
}


public void run() throws Exception {

    Socket mySkt = new Socket("192.168.1.6", 9999);

     PrintStream myPS = new PrintStream(mySkt.getOutputStream());

     myPS.println("Hello server");

     BufferedReader myBR = new BufferedReader
    (new InputStreamReader(mySkt.getInputStream()));


}

}

and this in netBeans:

//Author: WarLordTech
//Website: http://www.youtube.com/user/WarLordTech
import java.io.*;
import java.net.*;

public class Server {
  public static void main(String[] args) throws Exception{
      Server myServ = new Server();
      myServ.run();
  }

  public void run() throws Exception{
      ServerSocket mySS = new ServerSocket(9999);
      Socket SS_accept = mySS.accept();

      BufferedReader SS_BF= new BufferedReader(new InputStreamReader
        (SS_accept.getInputStream()));

      String temp = SS_BF.readLine();
      System.out.println(temp);

      if (temp!=null) {
        PrintStream SSPS = new PrintStream(SS_accept.getOutputStream());
        SSPS.println("Got something");
      }
   }
}

It still isnt workiong. Do I have to set up my network somehow?

4

1 に答える 1

0

TCPソケットを使用してそれを行うことができます。多くの言語には Socket プログラミングの実装があるため、Java でデスクトップ アプリをプログラミングする必要があります (もちろん、それは常に可能です!)。

Java のソケット チュートリアル

これは、ネットおよびローカル Wi-Fi 経由で機能します。UDP 接続など、ローカル Wi-Fi には他の方法を使用できます。TCPサーバーをセットアップし、アクセスできることを確認する必要があります。

これを行う方法は他にもあるかもしれませんが、それほど簡単な作業ではありません。

于 2013-03-12T16:11:52.400 に答える