首页 > 解决方案 > 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

标签: javaandroidnode.jsandroid-studioandroid-layout

解决方案


推荐阅读