Βαγγέλης Ανδρεάδης
- Forum posts: 3
Nov 29, 2012, 7:24:41 PM via Website
Nov 29, 2012 7:24:41 PM via Website
Hellow i am new here and i start to make apps for android , in my second apps i make a chat for ,facebook , msn, and gmail but i cant connect and chat and i don understand why the app run in phone but i give username and password i cant conect and app is force closed..
here is the code
here is the code
1package com.example.multimedia;
2
3
4import java.util.ArrayList;
5
6import com.example.multimedia.R;
7import android.app.Dialog;
8import android.util.Log;
9import android.view.View;
10import android.view.Window;
11import android.view.WindowManager;
12import android.webkit.WebView;
13import android.widget.ArrayAdapter;
14import android.widget.AutoCompleteTextView;
15import android.widget.Button;
16import android.widget.EditText;
17import android.widget.Spinner;
18import android.widget.TextView;
19
20import org.jivesoftware.smack.ConnectionConfiguration;
21import org.jivesoftware.smack.XMPPConnection;
22import org.jivesoftware.smack.XMPPException;
23import org.jivesoftware.smack.packet.Presence;
24
25
26public class xmppsettings extends Dialog implements android.view.View.OnClickListener {
27 private ChatActivity chatClient;
28 Spinner service;
29 TextView username;
30 TextView password;
31 Button set;
32 // private String[] srv =new String [] {"facebook", "msn", "gmail"};
33 String host,servs;
34 int port;
35
36 public xmppsettings(ChatActivity chatClient){
37 super(chatClient);
38 this.chatClient = chatClient;
39 }
40
41 protected void onStart() {
42 super.onStart();
43 getWindow().setFlags(4,4);
44 setContentView(R.layout.xmppsettings);
45 setTitle("Connection Settings");
46 Button ok = (Button) findViewById(R.id.ok);
47 ok.setOnClickListener(this);
48 }
49
50 public void onClick(View v) {
51
52 String[] srv ={"facebook", "msn", "gmail"};
53 service = (Spinner)findViewById(R.id.service);
54 username = (TextView)findViewById(R.id.usname);
55 String usna = username.getText().toString();
56 password = (TextView)findViewById(R.id.pasword);
57 String pass = password.getText().toString();
58 set = (Button) findViewById(R.id.set);
59
60
61 ArrayList<String> list = new ArrayList<String>();
62
63 //create an ArrayAdaptar from the String Array
64 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(xmppsettings.this.getContext(), android.R.layout.simple_spinner_item, list);
65 //set the view for the Drop down list
66 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
67 //set the ArrayAdapter to the spinner
68 service.setAdapter(dataAdapter);
69
70 set.setOnClickListener(new View.OnClickListener() {
71 public void onClick(View v) {
72 String o = (String) service.getSelectedItem ();
73
74 if (o == "facebook") {
75 host="chat.facebook.com";
76 port=Integer.parseInt("5222");
77 servs="facebook";
78 }
79 else if (o=="msn"){
80 host="messenger.hotmail.com";
81 port=Integer.parseInt("1863");
82 servs="msn";
83 }
84 else if (o=="gmail"){
85 host="smtp.gmail.com";
86 port=Integer.parseInt("465");
87 servs="gmail";
88 }
89
90
91 } } );
92
93 ConnectionConfiguration connectionConfig =
94 new ConnectionConfiguration(host, port, servs);
95 XMPPConnection connection = new XMPPConnection(connectionConfig);
96
97 try {
98 connection.connect();
99 Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
100 } catch (XMPPException ex) {
101 Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
102 chatClient.setConnection(null);
103 }
104 try {
105 connection.login(usna, pass);
106 Log.i("XMPPClient", "Logged in as " + connection.getUser());
107 // Set status to online / available
108 Presence presence = new Presence(Presence.Type.available);
109 connection.sendPacket(presence);
110 chatClient.setConnection(connection);
111 } catch (XMPPException ex) {
112 Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
113 chatClient.setConnection(null);
114 }
115 dismiss();
116 }
117
118
119
120
121 private String getText(int id) {
122 EditText widget = (EditText) this.findViewById(id);
123 return widget.getText().toString();
124 }
125}
2
3
4import java.util.ArrayList;
5
6import com.example.multimedia.R;
7import android.app.Dialog;
8import android.util.Log;
9import android.view.View;
10import android.view.Window;
11import android.view.WindowManager;
12import android.webkit.WebView;
13import android.widget.ArrayAdapter;
14import android.widget.AutoCompleteTextView;
15import android.widget.Button;
16import android.widget.EditText;
17import android.widget.Spinner;
18import android.widget.TextView;
19
20import org.jivesoftware.smack.ConnectionConfiguration;
21import org.jivesoftware.smack.XMPPConnection;
22import org.jivesoftware.smack.XMPPException;
23import org.jivesoftware.smack.packet.Presence;
24
25
26public class xmppsettings extends Dialog implements android.view.View.OnClickListener {
27 private ChatActivity chatClient;
28 Spinner service;
29 TextView username;
30 TextView password;
31 Button set;
32 // private String[] srv =new String [] {"facebook", "msn", "gmail"};
33 String host,servs;
34 int port;
35
36 public xmppsettings(ChatActivity chatClient){
37 super(chatClient);
38 this.chatClient = chatClient;
39 }
40
41 protected void onStart() {
42 super.onStart();
43 getWindow().setFlags(4,4);
44 setContentView(R.layout.xmppsettings);
45 setTitle("Connection Settings");
46 Button ok = (Button) findViewById(R.id.ok);
47 ok.setOnClickListener(this);
48 }
49
50 public void onClick(View v) {
51
52 String[] srv ={"facebook", "msn", "gmail"};
53 service = (Spinner)findViewById(R.id.service);
54 username = (TextView)findViewById(R.id.usname);
55 String usna = username.getText().toString();
56 password = (TextView)findViewById(R.id.pasword);
57 String pass = password.getText().toString();
58 set = (Button) findViewById(R.id.set);
59
60
61 ArrayList<String> list = new ArrayList<String>();
62
63 //create an ArrayAdaptar from the String Array
64 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(xmppsettings.this.getContext(), android.R.layout.simple_spinner_item, list);
65 //set the view for the Drop down list
66 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
67 //set the ArrayAdapter to the spinner
68 service.setAdapter(dataAdapter);
69
70 set.setOnClickListener(new View.OnClickListener() {
71 public void onClick(View v) {
72 String o = (String) service.getSelectedItem ();
73
74 if (o == "facebook") {
75 host="chat.facebook.com";
76 port=Integer.parseInt("5222");
77 servs="facebook";
78 }
79 else if (o=="msn"){
80 host="messenger.hotmail.com";
81 port=Integer.parseInt("1863");
82 servs="msn";
83 }
84 else if (o=="gmail"){
85 host="smtp.gmail.com";
86 port=Integer.parseInt("465");
87 servs="gmail";
88 }
89
90
91 } } );
92
93 ConnectionConfiguration connectionConfig =
94 new ConnectionConfiguration(host, port, servs);
95 XMPPConnection connection = new XMPPConnection(connectionConfig);
96
97 try {
98 connection.connect();
99 Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
100 } catch (XMPPException ex) {
101 Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
102 chatClient.setConnection(null);
103 }
104 try {
105 connection.login(usna, pass);
106 Log.i("XMPPClient", "Logged in as " + connection.getUser());
107 // Set status to online / available
108 Presence presence = new Presence(Presence.Type.available);
109 connection.sendPacket(presence);
110 chatClient.setConnection(connection);
111 } catch (XMPPException ex) {
112 Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
113 chatClient.setConnection(null);
114 }
115 dismiss();
116 }
117
118
119
120
121 private String getText(int id) {
122 EditText widget = (EditText) this.findViewById(id);
123 return widget.getText().toString();
124 }
125}
1package com.example.multimedia;
2
3import android.app.Activity;
4import java.util.ArrayList;
5import java.util.List;
6import com.example.multimedia.R;
7import android.os.Bundle;
8import android.os.Handler;
9import android.view.Menu;
10import android.view.MenuInflater;
11import android.view.MenuItem;
12import android.view.View;
13import android.widget.AdapterView;
14import android.widget.ArrayAdapter;
15import android.widget.Button;
16import android.widget.EditText;
17import android.widget.ListView;
18import android.widget.Toast;
19
20import org.jivesoftware.smack.ConnectionConfiguration;
21import org.jivesoftware.smack.PacketListener;
22import org.jivesoftware.smack.XMPPConnection;
23import org.jivesoftware.smack.XMPPException;
24import org.jivesoftware.smack.filter.MessageTypeFilter;
25import org.jivesoftware.smack.filter.PacketFilter;
26import org.jivesoftware.smack.packet.Message;
27import org.jivesoftware.smack.packet.Packet;
28import org.jivesoftware.smack.packet.Presence;
29import org.jivesoftware.smack.util.StringUtils;
30import android.content.Intent;
31import android.util.Log;
32import android.widget.TextView;
33
34
35public class ChatActivity extends Activity {
36
37 private ArrayList<String> messages = new ArrayList();
38 private Handler handler = new Handler();
39 private xmppsettings settings;
40 private EditText recipient;
41 private EditText text;
42 private ListView list;
43 private XMPPConnection connection;
44
45 @Override
46 public void onCreate(Bundle savedInstanceState) {
47 super.onCreate(savedInstanceState);
48 setContentView(R.layout.chat_layout);
49
50 recipient = (EditText) this.findViewById(R.id.recipient);
51 text = (EditText) this.findViewById(R.id.text);
52 list = (ListView) this.findViewById(R.id.messageList);
53 setListAdapter();
54
55
56 settings = new xmppsettings(this);
57
58 Button setup = (Button) this.findViewById(R.id.settings);
59
60 setup.setOnClickListener(new View.OnClickListener() {
61 public void onClick(View view) {
62 handler.post(new Runnable() {
63 public void run() {
64 settings.show();
65 //settings.dismiss();
66 }
67 });
68 }
69 });
70
71 Button send = (Button) this.findViewById(R.id.send);
72
73 send.setOnClickListener(new View.OnClickListener() {
74
75 public void onClick(View v) {
76 String to = recipient.getText().toString();
77 String text1 = text.getText().toString();
78
79 Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
80 Message msg = new Message(to, Message.Type.chat);// error
81 msg.setBody(text1);
82 connection.sendPacket(msg);
83 messages.add(connection.getUser() + ":");// error
84 messages.add(text1);
85 setListAdapter();
86 }
87 });
88 }
89
90 //Called by settings when connection is established
91
92 public void setConnection (XMPPConnection connection) {
93 this.connection = connection;
94 if (connection != null) {
95 //Packet listener to get messages sent to logged in user
96 PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
97 connection.addPacketListener(new PacketListener() {
98 public void processPacket(Packet packet) {
99 Message message = (Message) packet;
100 if (message.getBody() != null) {
101 String fromName = StringUtils.parseBareAddress(message.getFrom()); // error
102 messages.add(fromName + ":");
103 messages.add(message.getBody());
104 handler.post(new Runnable(){
105 public void run() {
106 setListAdapter();
107 }
108 });
109 }
110 }
111 }, filter);
112 }
113 }
114
115 private void setListAdapter() {
116 ArrayAdapter<String> adapter = new ArrayAdapter<String>
117 (this, R.layout.chat_layout, messages);
118 list.setAdapter(adapter);
119 }
120
121}
2
3import android.app.Activity;
4import java.util.ArrayList;
5import java.util.List;
6import com.example.multimedia.R;
7import android.os.Bundle;
8import android.os.Handler;
9import android.view.Menu;
10import android.view.MenuInflater;
11import android.view.MenuItem;
12import android.view.View;
13import android.widget.AdapterView;
14import android.widget.ArrayAdapter;
15import android.widget.Button;
16import android.widget.EditText;
17import android.widget.ListView;
18import android.widget.Toast;
19
20import org.jivesoftware.smack.ConnectionConfiguration;
21import org.jivesoftware.smack.PacketListener;
22import org.jivesoftware.smack.XMPPConnection;
23import org.jivesoftware.smack.XMPPException;
24import org.jivesoftware.smack.filter.MessageTypeFilter;
25import org.jivesoftware.smack.filter.PacketFilter;
26import org.jivesoftware.smack.packet.Message;
27import org.jivesoftware.smack.packet.Packet;
28import org.jivesoftware.smack.packet.Presence;
29import org.jivesoftware.smack.util.StringUtils;
30import android.content.Intent;
31import android.util.Log;
32import android.widget.TextView;
33
34
35public class ChatActivity extends Activity {
36
37 private ArrayList<String> messages = new ArrayList();
38 private Handler handler = new Handler();
39 private xmppsettings settings;
40 private EditText recipient;
41 private EditText text;
42 private ListView list;
43 private XMPPConnection connection;
44
45 @Override
46 public void onCreate(Bundle savedInstanceState) {
47 super.onCreate(savedInstanceState);
48 setContentView(R.layout.chat_layout);
49
50 recipient = (EditText) this.findViewById(R.id.recipient);
51 text = (EditText) this.findViewById(R.id.text);
52 list = (ListView) this.findViewById(R.id.messageList);
53 setListAdapter();
54
55
56 settings = new xmppsettings(this);
57
58 Button setup = (Button) this.findViewById(R.id.settings);
59
60 setup.setOnClickListener(new View.OnClickListener() {
61 public void onClick(View view) {
62 handler.post(new Runnable() {
63 public void run() {
64 settings.show();
65 //settings.dismiss();
66 }
67 });
68 }
69 });
70
71 Button send = (Button) this.findViewById(R.id.send);
72
73 send.setOnClickListener(new View.OnClickListener() {
74
75 public void onClick(View v) {
76 String to = recipient.getText().toString();
77 String text1 = text.getText().toString();
78
79 Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
80 Message msg = new Message(to, Message.Type.chat);// error
81 msg.setBody(text1);
82 connection.sendPacket(msg);
83 messages.add(connection.getUser() + ":");// error
84 messages.add(text1);
85 setListAdapter();
86 }
87 });
88 }
89
90 //Called by settings when connection is established
91
92 public void setConnection (XMPPConnection connection) {
93 this.connection = connection;
94 if (connection != null) {
95 //Packet listener to get messages sent to logged in user
96 PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
97 connection.addPacketListener(new PacketListener() {
98 public void processPacket(Packet packet) {
99 Message message = (Message) packet;
100 if (message.getBody() != null) {
101 String fromName = StringUtils.parseBareAddress(message.getFrom()); // error
102 messages.add(fromName + ":");
103 messages.add(message.getBody());
104 handler.post(new Runnable(){
105 public void run() {
106 setListAdapter();
107 }
108 });
109 }
110 }
111 }, filter);
112 }
113 }
114
115 private void setListAdapter() {
116 ArrayAdapter<String> adapter = new ArrayAdapter<String>
117 (this, R.layout.chat_layout, messages);
118 list.setAdapter(adapter);
119 }
120
121}
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.