我想從Android的字符串發送到我的電腦(Visual Basic中2010)我嘗試這一個,但它不工作。請人幫助我..從機器人發送字符串到Visual Basic 2010
我從拿到我的Android代碼: http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/
而且它的樣子:
package com.zelacroix.bukumenu;
import java.io.*;
import java.net.*;
import android.app.Activity;
import android.os.*;
import android.util.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class TesKirim extends Activity {
private EditText serverIp;
private Button connectPhones;
private String serverIpAddress = "192.168.1.2";
private boolean connected = false;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teskirim);
serverIp = (EditText) findViewById(R.id.server_ip);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
Toast.makeText(getApplicationContext(),
"masuk if",
10)
.show();
cThread.start();
}
}
}
});
};
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr =
InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr , 10000);
connected = true;
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),
true);
// where you issue the commands
out.println("Hey Server!");
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
};
};
我的Visual Basic中使用UDP方法。而它的樣子:
Imports System.Net.Sockets
Imports System.Net
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim udp_Client As New UdpClient(10000)
Dim remoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim receiveBytes As [Byte]() = udp_Client.Receive(remoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
ListBox1.Items.Add(returnData.ToString)
udp_Client.Close()
Catch ex As Exception
ListBox1.Items.Add(ex.ToString())
End Try
End Sub
End Class
請我really2需要comunicate他們..
請告訴我問題你都面臨着? – user370305
問題在哪裏?你的android不發送數據?或者,也許你的VB不收到它?也許字符串編碼有問題? 如果字符串被髮送,你使用Wireshark檢查過嗎? – Zuljin
首先,我的問題..我的Android只是運行到Log.d(「ClientActivity」,「C:連接...」);所以我檢查了DDMS日誌。然後代碼不能進一步proccess :(什麼錯呢?我得到了錯誤的東西說,像家人不支持。 – Michael