java - Android Studio - Socket.io TextView 没有为每个创建(没有错误)
问题描述
我已经在做一个 Messenger 并且我是 android studio 的新手(抱歉我的英语不好)
我没有收到任何错误,但没有为发送的消息创建文本视图(发送的消息日志仅发送到 nodejs 服务器控制台
MainAcitvity.java
package me.bash.tellogrambeta;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class MainActivity extends AppCompatActivity {
private Socket socket;
{
try {
socket = IO.socket("http://192.168.1.199:8000");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
Button btnSend;
EditText edtTextMessage;
LinearLayout linearMessage;
LinearLayout.LayoutParams layoutParams;
public Handler handler;
@Override
protected void onDestroy() {
super.onDestroy();
socket.disconnect();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSend = (Button)findViewById(R.id.btnSend);
handler = new Handler();
edtTextMessage = (EditText)findViewById(R.id.edtTextMessage);
linearMessage=(LinearLayout)findViewById(R.id.linearMessage);
socket.connect();
socket.on("message",handlerIncomingMessage);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = edtTextMessage.getText().toString();
sendMessage(message);
}
});
}
public Emitter.Listener handlerIncomingMessage = new Emitter.Listener() {
@Override
public void call(Object... args) {
handler.post(new Runnable() {
@Override
public void run() {
JSONObject jsonObject=(JSONObject) args[0];
String message = "";
try {
message = jsonObject.getString("message").toString();
TextView textView = new TextView(getApplicationContext());
textView.setText(message);
textView.setTextSize(18);
layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearMessage.addView(textView);
} catch (JSONException e){
e.printStackTrace();
}
}
});
}
};
private void sendMessage(String message){
socket.emit("message",message);
}
}
节点 JS Server.js
var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);
app.get("/", function(req,res,next) {
res.sendFile(__dirname+"/static/index.html");
});
io.on("connection", function (socket){
console.log(" One Ping Recerved From Tellogram by ID " + socket.id);
socket.on("message", function (data){
console.log(data);
var sockets = io.sockets.sockets;
sockets.forEach(function (item) {
item.emit("message", {message:data});
});
});
socket.on("disconnect", function(){
console.log("User Disconnected from Server");
});
});
http.listen(8000);
console.log("Server Runned and Working on 8000 port")
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.bash.tellogrambeta">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TellogramBeta">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请帮助我快点;( 我需要帮助来解决这个问题
谢谢大家<3
解决方案
推荐阅读
- java - HttpMessageNotReadableException:JSON 解析错误:无法构造实例
- amazon-web-services - 修改 EC2 服务角色,使其可由同一账户中的 IAM 用户代入
- automated-tests - TestCafe 铬测试作为 docker build 的一部分运行
- apache-spark - Spark Streaming中批处理时间和提交时间相差50分钟
- docker - Docker 容器中的 Azure 函数
- excel - 匹配两张纸中的单元格并复制粘贴匹配内容
- android - 带有 Json 的 Android BuildConfig 字符串
- visual-studio - Visual Studio 2015 Enterprise 卸载、重装、更改失败
- html - 如何根据布尔值向 html/ionic 组件添加或删除属性?
- react-native - createStackNavigator 返回未定义