如何在android中制作聊天服务器和客户端

发布于 2024-11-06 04:52:40 字数 3821 浏览 1 评论 0原文

我想在android中制作一个聊天应用程序。我正在使用套接字将其制作为简单的 java 应用程序,但它不起作用。请建议我应该做什么。

这是我的代码。单击按钮后,应用程序将强制关闭。

package pack.chat;

import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences.Editor;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.io.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;



public class chatAct extends Activity implements android.view.View.OnClickListener {
    /** Called when the activity is first created. */

    PrintWriter out;
    BufferedReader in ;
    Socket socketClient;
    EditText edit1;
    String editTextString;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try{
             socketClient= new Socket( "hddlntd6014578 ", 8180); 
             out = new PrintWriter(socketClient.getOutputStream(), true);
             in = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
        }
       catch (UnknownHostException e){
           System.out.println("Host cannt be reached");
       }
       catch (IOException i){
           System.out.println("IO cannt be found");
       }


       Button searchButton = (Button)findViewById(R.id.Button01);
       searchButton.setOnClickListener(this);

      // InputStreamReader input= new InputStreamReader();
     //  BufferedReader stringForServer= new BufferedReader(new InputStreamReader(R.id.EditText01));

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        edit1= (EditText)findViewById(R.id.EditText01);

           System.out.println(edit1.getText().toString());

               String forServer = edit1.getText().toString();
           out.println(forServer);
    }
}

您可以提取我是一只新蜜蜂。请建议一些东西。如果您有代码并且可以的话请与我分享。

记录猫::

05-11 15:54:39.614: WARN/dalvikvm(370): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): FATAL EXCEPTION: main
05-11 15:54:39.624: ERROR/AndroidRuntime(370): java.lang.NullPointerException
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at pack.chat.chatAct.onClick(chatAct.java:61)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.view.View.performClick(View.java:2485)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.view.View$PerformClick.run(View.java:9080)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.os.Handler.handleCallback(Handler.java:587)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.os.Looper.loop(Looper.java:123)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.app.ActivityThread.main(ActivityThread.java:3647)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at java.lang.reflect.Method.invoke(Method.java:507)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at dalvik.system.NativeStart.main(Native Method)
05-11 15:54:39.634: WARN/ActivityManager(61):   Force finishing activity pack.chat/.chatAct
05-11 15:54:40.164: WARN/ActivityManager(61): Activity pause timeout for HistoryRecord{40672790 pack.chat/.chatAct}

I want to make an chat application in android. I was making it like simple java application using socket but it is not working. Pls suggest what I should do.

This is my code. On the click of button, app is closing forcely..

package pack.chat;

import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences.Editor;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.io.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;



public class chatAct extends Activity implements android.view.View.OnClickListener {
    /** Called when the activity is first created. */

    PrintWriter out;
    BufferedReader in ;
    Socket socketClient;
    EditText edit1;
    String editTextString;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try{
             socketClient= new Socket( "hddlntd6014578 ", 8180); 
             out = new PrintWriter(socketClient.getOutputStream(), true);
             in = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
        }
       catch (UnknownHostException e){
           System.out.println("Host cannt be reached");
       }
       catch (IOException i){
           System.out.println("IO cannt be found");
       }


       Button searchButton = (Button)findViewById(R.id.Button01);
       searchButton.setOnClickListener(this);

      // InputStreamReader input= new InputStreamReader();
     //  BufferedReader stringForServer= new BufferedReader(new InputStreamReader(R.id.EditText01));

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        edit1= (EditText)findViewById(R.id.EditText01);

           System.out.println(edit1.getText().toString());

               String forServer = edit1.getText().toString();
           out.println(forServer);
    }
}

You can extract that I am a new bee. Please suggest something. If you have a code and if u can then pls share that with me.

Log CAT::

05-11 15:54:39.614: WARN/dalvikvm(370): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-11 15:54:39.624: ERROR/AndroidRuntime(370): FATAL EXCEPTION: main
05-11 15:54:39.624: ERROR/AndroidRuntime(370): java.lang.NullPointerException
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at pack.chat.chatAct.onClick(chatAct.java:61)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.view.View.performClick(View.java:2485)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.view.View$PerformClick.run(View.java:9080)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.os.Handler.handleCallback(Handler.java:587)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.os.Looper.loop(Looper.java:123)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at android.app.ActivityThread.main(ActivityThread.java:3647)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at java.lang.reflect.Method.invoke(Method.java:507)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-11 15:54:39.624: ERROR/AndroidRuntime(370):     at dalvik.system.NativeStart.main(Native Method)
05-11 15:54:39.634: WARN/ActivityManager(61):   Force finishing activity pack.chat/.chatAct
05-11 15:54:40.164: WARN/ActivityManager(61): Activity pause timeout for HistoryRecord{40672790 pack.chat/.chatAct}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2024-11-13 04:52:40

您没有定义变量out。所以 out.print* 将是 NPE。
您可以在该行上使用 System.out.print* 或在使用它之前初始化 out

在查看代码时我也错过了这一点:) 如果您使用像 Eclipse 这样的 IDE,请尝试提高潜在 NPE 等的警告级别。

You don't have variable out defined. So out.print* will NPE.
You may use System.out.print* on that line or initialize out before using it.

I also missed this when looking over the code :) If you use an IDE like Eclipse, try increasing warning levels for potential NPEs and the like.

同展鸳鸯锦 2024-11-13 04:52:40

我已经制作了一个使用 C2DM(推送邮件)聊天的应用程序,这是在 Android 设备之间发送数据的新方法。我认为你应该使用 Android 的 C2DM 云到设备消息传递技术来制作此应用程序。
我希望这是有帮助的。

I have already make a application for chatting using C2DM (push mail) this is the mew way to send data among android devices. I think you should you C2DM cloud to device messaging technology of android for making this application.
I hope this is help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文