Android客户端无法正确向服务器发送消息

时间:2014-02-04 02:11:57

标签: java android sockets

我正在为Android设备上的客户端编写代码以向服务器发送消息,服务器应该回复它。布局由编辑文本字段,按钮和文本视图组成。当按下按钮时,应该从编辑文本字段中获取消息并将其发送到服务器,当服务器收到消息时,它应该回复一条消息,表明它已收到消息,然后该消息将由客户端和写在文本视图上。问题是当我按下按钮两次然后当我按第三次服务器崩溃时,消息被发送到服务器。任何有关问题的帮助都会受到赞赏。提前谢谢。

这是我得到的logcat错误

02-04 04:18:38.065: I/Error51(32228): android.os.NetworkOnMainThreadException

MainActivity.java

package com.example.testclientandroid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    static Socket socket;
    static final int SERVERPORT = 50000;
    static final String SERVER_IP = "192.168.0.105";

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

        new Thread(new ClientThread()).start();

        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText editText = (EditText) findViewById(R.id.editText1);
                TextView textView = (TextView) findViewById(R.id.textView2);
                try {
                    String str = editText.getText().toString();

                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    out.println(str);

                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    textView.setText(in.readLine());

                } catch (UnknownHostException e) {
                    Log.i("Error47", e.toString());
                } catch (IOException e) {
                    Log.i("Error49", e.toString());
                } catch (Exception e) {
                    Log.i("Error51", e.toString());
                }
            }
        });
    }

    private static class ClientThread implements Runnable {

        public void run() {

            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

                socket = new Socket(serverAddr, SERVERPORT);

            } catch (UnknownHostException e) {
                Log.i("Error64", e.toString());
            } catch (IOException e) {
                Log.i("Error65", e.toString());
            }
        }

    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="Send" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="53dp"
        android:text="Response:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1" />

</RelativeLayout>

Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testclientandroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testclientandroid.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

ServerClass.java

package mainPackage;
//Server
import java.net.*;
import java.io.*;

public class ServerClass
{

    static final int PORTNUMBER = 50000;

    public static void main(String[] args) throws IOException
    {
        new Thread(new ServerThread()).start();
    }

    public static class ServerThread implements Runnable {

        ServerSocket serverSocket;
        Socket clientSocket;
        public void run() {
            try {
                serverSocket = new ServerSocket(PORTNUMBER);
                clientSocket = serverSocket.accept();
                new Thread(new CommunicationThread(clientSocket)).start();

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

    public static class CommunicationThread implements Runnable {

        Socket socket;
        BufferedReader in;
        PrintWriter out;

        public CommunicationThread(Socket clientSocket) {

            socket = clientSocket;

            try {

                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                //out = new PrintWriter(clientSocket.getOutputStream(), true); 
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public void run() {
            try {

                while(in.readLine() != null)
                {
                    System.out.println(in.readLine());
                    out.println(in.readLine() + " Received");
                }

            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }
    }
}

更新 我尝试使用asyncTask,我不再收到Logcat错误,但现在服务器根本没有收到消息,

以下是修改后的代码:

package com.example.testclientandroid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    static Socket socket;
    static final int SERVERPORT = 50000;
    static final String SERVER_IP = "192.168.0.105";

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

        new Thread(new ClientThread()).start();

        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                new CommunicationTask().execute();
            }
        });
    }

    private static class ClientThread implements Runnable {

        public void run() {

            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

                socket = new Socket(serverAddr, SERVERPORT);

            } catch (UnknownHostException e) {
                Log.i("Error64", e.toString());
            } catch (IOException e) {
                Log.i("Error65", e.toString());
            }
        }
    }

    private class CommunicationTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            EditText editText = (EditText) findViewById(R.id.editText1);
            String result;
            try {
                String str = editText.getText().toString();

                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                out.println(str);

                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                result = in.readLine();
                return result;
            } catch (UnknownHostException e) {
                Log.i("Error47", e.toString());
            } catch (IOException e) {
                Log.i("Error49", e.toString());
            } catch (Exception e) {
                Log.i("Error51", e.toString());
            }
            return "Error";
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            TextView textView = (TextView) findViewById(R.id.textView2);
            textView.setText(result);
        }   
    }
}

2 个答案:

答案 0 :(得分:0)

任何需要互联网的操作都应该在后台进行。尝试阅读Asynctask以删除此错误。

答案 1 :(得分:0)

尝试使用asynctask,它简单易用.AsyncTask可以正确,方便地使用UI线程。此类允许执行后台操作并在UI线程上发布结果,而无需操作线程和/或处理程序。

这是一个例子     私有类DownloadFilesTask扩展AsyncTask {     protected long doInBackground(URL ... urls){     int count = urls.length;     long totalSize = 0;     for(int i = 0; i&lt; count; i ++){     totalSize + = Downloader.downloadFile(urls [i]);     publishProgress((int)((i /(float)count)* 100));     //如果调用cancel(),则提前退出     if(isCancelled())中断;     }     return totalSize;     }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }

}