如何在android中制作聊天服务器和客户端
我想在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有定义变量
out
。所以out.print*
将是 NPE。您可以在该行上使用
System.out.print*
或在使用它之前初始化out
。在查看代码时我也错过了这一点:) 如果您使用像 Eclipse 这样的 IDE,请尝试提高潜在 NPE 等的警告级别。
You don't have variable
out
defined. Soout.print*
will NPE.You may use
System.out.print*
on that line or initializeout
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.
我已经制作了一个使用 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.