聊天程序自动关闭

时间:2017-05-02 00:05:19

标签: java android sockets networking

我一直在尝试学习一些Android开发,而且我一直试图进入某种网络,以便我制作的应用可以基于网络。无论如何,我开始尝试制作一个使用手机或笔记本电脑作为客户端的聊天程序,并在我的笔记本电脑上运行服务器。我的问题是每当我尝试向服务器发送消息时,我的应用程序就会关闭。我在考虑可能性,但我不确定实际发生了什么。

无论如何这里是android代码:

package com.example.android.chatprogram;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendMessage()
    {
        try {

            TextView inp = (TextView) findViewById(R.id.msg_box);
            TextView receive = (TextView) findViewById(R.id.chat_window);
            Socket clientSocket = new Socket("localhost", 9002); //99.228.211.122
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            String msg = inp.getText().toString();
            outToServer.writeBytes(msg + '\n');
            receive.append(inFromServer.readLine());
            clientSocket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

这是服务器代码(在我的笔记本电脑上运行):

import java.io.*;
import java.net.*;

public class server {
    public static void main(String args[]) throws Exception {
        String clientSentence;
        String capitalizedSentence;
        ServerSocket welcomeSocket = new ServerSocket(9001);

        while(true) {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println(clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
        }
    }

提前谢谢!

编辑:我终于找到了堆栈跟踪了!

05-0121:48:04.74828076-28076/com.example.android.chatprogramE/AndroidRuntime:
FATALEXCEPTION:main
Process:com.example.android.chatprogram,PID:28076
java.lang.IllegalStateException:Couldnotexecutemethodforandroid:onClick
atandroid.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
atandroid.view.View.performClick(View.java:5623)
atandroid.view.View$PerformClick.run(View.java:22433)
atandroid.os.Handler.handleCallback(Handler.java:751)
atandroid.os.Handler.dispatchMessage(Handler.java:95)
atandroid.os.Looper.loop(Looper.java:154)
atandroid.app.ActivityThread.main(ActivityThread.java:6247)
atjava.lang.reflect.Method.invoke(NativeMethod)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Causedby:java.lang.reflect.InvocationTargetException
atjava.lang.reflect.Method.invoke(NativeMethod)
atandroid.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
atandroid.view.View.performClick(View.java:5623) 
atandroid.view.View$PerformClick.run(View.java:22433) 
atandroid.os.Handler.handleCallback(Handler.java:751) 
atandroid.os.Handler.dispatchMessage(Handler.java:95) 
atandroid.os.Looper.loop(Looper.java:154) 
atandroid.app.ActivityThread.main(ActivityThread.java:6247) 
atjava.lang.reflect.Method.invoke(NativeMethod) 
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
Causedby:android.os.NetworkOnMainThreadException
atandroid.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
atjava.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:333)
atjava.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
atjava.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
atjava.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
atjava.net.Socket.connect(Socket.java:586)
atjava.net.Socket.connect(Socket.java:535)
atjava.net.Socket.<init>(Socket.java:427)
atjava.net.Socket.<init>(Socket.java:210)
atcom.example.android.chatprogram.MainActivity.sendMessage(MainActivity.java:29)
atjava.lang.reflect.Method.invoke(NativeMethod) 
atandroid.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
atandroid.view.View.performClick(View.java:5623) 
atandroid.view.View$PerformClick.run(View.java:22433) 
atandroid.os.Handler.handleCallback(Handler.java:751) 
atandroid.os.Handler.dispatchMessage(Handler.java:95) 
atandroid.os.Looper.loop(Looper.java:154) 
atandroid.app.ActivityThread.main(ActivityThread.java:6247) 
atjava.lang.reflect.Method.invoke(NativeMethod) 
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

1 个答案:

答案 0 :(得分:0)

首先,当您遇到崩溃时,请始终发布堆栈跟踪。它告诉你什么是失败,为什么,以及在哪里。

在这种情况下,它将成为NetworkOnMainThreadException。所有网络呼叫必须在另一个线程上完成。