android socketio,如何将Android应用程序连接到pythonsocketio后端?

本文探讨了如何解决Android应用尝试连接Python SocketIO服务器时遭遇的SSL握手失败问题,重点介绍了从Android客户端的角度调整代码以适应Python后端,并提供了可能的解决方案和错误排查步骤。
摘要由CSDN通过智能技术生成

我目前正在运行一个pythonsocketio服务器,它可以完美地连接到我的JavaScript客户端。我使用socketio android example chat app编写Android代码,它与NodeJS服务器完美配合,但是当我切换到使用Python服务器时,它将无法连接。在

如何从Android连接到Ptyhon SocketIO服务器?

Android代码:public class HomeActivity extends AppCompatActivity

implements NavigationView.OnNavigationItemSelectedListener {

private final String TAG = "MainActivity";

Button btnCore0, btnCore1, btnCPUUsage;

private ProgressBar progressBar;

private Socket mSocket;

{

try {

mSocket = IO.socket(Constants.SERVER_URL);

} catch (URISyntaxException e) {

Log.e(TAG, e.getMessage());

}

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_home);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

.setAction("Action", null).show();

}

});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(

this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

drawer.setDrawerListener(toggle);

toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

navigationView.setNavigationItemSelectedListener(this);

btnCore0 = (Button) findViewById(R.id.btnCore0);

btnCore1 = (Button) findViewById(R.id.btnCore1);

btnCPUUsage = (Button) findViewById(R.id.btnCPUUsage);

progressBar = (ProgressBar) findViewById(R.id.progressBar);

// Make buttons invisible

btnCore0.setVisibility(View.INVISIBLE);

btnCore1.setVisibility(View.INVISIBLE);

btnCPUUsage.setVisibility(View.INVISIBLE);

// Make progress bar visible

progressBar.setVisibility(View.VISIBLE);

mSocket.on("status-update", onNewMessage);

mSocket.on(Socket.EVENT_DISCONNECT, onSocketDisconnected);

mSocket.connect();

}

private Emitter.Listener onNewMessage = new Emitter.Listener() {

@Override

public void call(final Object... args) {

HomeActivity.this.runOnUiThread(new Runnable() {

@Override

public void run() {

Log.d(TAG, "New message 090909***");

JSONObject data = (JSONObject) args[0];

int core0 = 0;

int core1 = 0;

int cpu_usage_in = 0;

try {

core0 = data.getInt("core0_in");

core1 = data.getInt("core1_in");

cpu_usage_in = data.getInt("cpu_usage_in");

} catch (JSONException e) {

Log.e(TAG, e.getMessage());

}

btnCore0.setText(getResources().getString(R.string.core0, String.valueOf(core0)));

btnCore1.setText(getResources().getString(R.string.core1, String.valueOf(core1)));

btnCPUUsage.setText(getResources().getString(R.string.cpu_usge, String.valueOf(cpu_usage_in)));

updateButtonBackgroundColor(btnCore0, core0);

updateButtonBackgroundColor(btnCore1, core1);

updateButtonBackgroundColor(btnCPUUsage, cpu_usage_in);

onServerDataReceived();

}

});

}

};

下一个是每秒发出的pyton服务器。我知道这很好,因为我可以从JavaScript应用程序连接到它。

Python代码:

^{pr2}$

出现以下错误:

SSLError:[SSL:SSL\u HANDSHAKE_FAILURE]SSL握手失败(_SSL.c:1754)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值