我試圖開發一個Android應用程序,我陷入了這個問題。從一個Android設備獲取字符串併發送到其他設備
我想從一個Android設備發送兩個字符串值(經度,緯度)到具有相同應用的其他Android設備。從在線研究,我的理解是,Android設備將兩個字符串發送到服務器,然後我可以使用谷歌gcm將這些字符串發送到其他設備(我認爲)。也許服務器可以運行java,我可以編寫一些程序來處理稍後收到的數據。
我已經成功地從android設備獲取位置數據,,但被卡在發送這個位置從android手機到我的電腦上的服務器。我的教授告訴我,我可以讓我的電腦成爲一臺服務器。在連接android和我的mac之前,我試着連接兩臺電腦。我成功地使用Socket &連接我的imac和macbook,在http://www.tutorialspoint.com/java/java_networking.htm的底部找到SocketServer示例。這花了很長時間,因爲我不知道我必須移除防火牆。
因此,我發現了另一個示例,將Android連接到使用Socket的電腦http://lakjeewa.blogspot.ca/2012/03/simple-client-server-application-for.html並嘗試使用它。儘管可以連接兩臺電腦,但android不會連接到我的電腦。我認爲這個問題是在代碼
client = new Socket(serverName, port);
我想對於servername很多東西,如計算機的IP地址以192.168開始,「本地主機」,10.0.2.2,HTTP:/ myIpAdressFoundByGoogling,「myComputerName.local」但沒有工作。這裏有什麼問題? Android設備沒有SIM卡,並且連接到與計算機相同的WiFi。我應該有XAAMPP? (我剛剛發現這個昨晚)並首先學習php?我完全失去了如何處理服務器。
我結束了這裏,因爲我想我可以測試和開發應用程序使用計算機作爲服務器,當我完成開發時,我可以得到一個服務器(如亞馬遜網絡服務,我不知道),並取代代碼中的服務器位置,並複製並粘貼服務器Java代碼。 我可以這樣做嗎?也許谷歌應用引擎是一個可行的解決我的問題?
的客戶端代碼......
public class MainActivity extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText textField;
private Button button;
private String message;
private int port = 5555;
private String serverName = "192.168.?????"; I think this is where the problem is
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textField = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
message = textField.getText().toString();
textField.setText("");
try {
// connect with server
client = new Socket(serverName, port);
printwriter = new PrintWriter(client.getOutputStream(),
true);
printwriter.write(message);
printwriter.flush();
printwriter.close();
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
服務器代碼...
public class main {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
public static void main(String[] args) {
int port = 5555;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
System.out.println("could not listen on port: " + port);
e.printStackTrace();
}
System.out.println("server started listening to the port " + port);
while (true) {
try {
// accept the client connection
clientSocket =serverSocket.accept();
System.out.println("someone connected to the server");
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
//get the client meesage
bufferedReader = new BufferedReader(inputStreamReader);
message = bufferedReader.readLine();
System.out.println(message);
inputStreamReader.close();
clientSocket.close();
} catch (IOException e) {
System.out.println("problem in reading the message");
e.printStackTrace();
}
}
}
我上傳了代碼。你可以看一下嗎? – WarmInToronto
你是否收到異常/崩潰?還是代碼執行得很好,只是消息沒有被髮送? – Bob
我剛剛收到「不幸的是,您的應用程序已停止。」 FATAL EXCEPTION,android.os.NetworkOnMainThreadException和LogCat中的其他紅線。 – WarmInToronto