I am trying to implement a chat application in android. The clients(smartphones) will communicate through a server installed in a computer. I have no idea about client-server communication, so I am desparetly need your help. I have read a lot of about and i've come to the conclusion that i have to use the HTTP(request/response), is this right? In HTTP the client/server send requests and the server/client send responses respectively, is this the way it works? Sockets is another way of impementation, i mean i don't need any sockets for the HTTP ? Sorry, i am asking all these but i am really confused. Do I need also any third party software? I have put the client(android application) and the server(java application) in totally different projects,packages. I have also use the following code for the login form:
public void tryLogin() {
//The Android logging system provides a mechanism for collecting and viewing system debug output. Logcat dumps a log of system messages, which include things such as stack traces when the emulator throws an error and messages that you have written from your application by using the Log class.
Log.v(TAG, "Trying to Login");//TAG used to identify the source of a log message. It usually identifies the class or activity where the log call occurs./second param The message you would like logged.
EditText etxt_user = (EditText) findViewById(R.id.username);//finds the username TextView
EditText etxt_pass = (EditText) findViewById(R.id.password);//finds the pasword TextView
String username1 = etxt_user.getText().toString();//gets the text that the user has typed as his/her username
String password1 = etxt_pass.getText().toString();//gets the text that the user haw typed as his/her password
DefaultHttpClient client = new DefaultHttpClient();//creates a DefaultHttpClient object
HttpPost httppost = new HttpPost("http://.......");
//add your Data
List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
nvps.add(new BasicNameValuePair("username", username1));
nvps.add(new BasicNameValuePair("password", password1));
try {
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);//The message-body (if any) of an HTTP message is used to carry the entity-body associated with the request or response. The message-body differs from the entity-body only when a transfer-coding has been applied, as indicated by the Transfer-Encoding header field
httppost.setEntity(p_entity);
//Execute HTTP Post Request
HttpResponse response = client.execute(httppost);
Log.v(TAG, response.getStatusLine().toString());//HTTP/1.1 200 OK --> see DDMS //HTTP has been in use by the World-Wide Web global information initiative since 1990. This specification defines the protocol referred to as "HTTP/1.1", and is an update to RFC 2068 [33].//OK 200-->The request was fulfilled.
//The message-body (if any) of an HTTP message is used to carry the entity-body associated with the request or response.
HttpEntity responseEntity = response.getEntity();
Log.v(TAG, "Set response to responseEntity" + EntityUtils.toString(responseEntity));
//SAXParserFactory --> defines a factory API that enables applications to configure and obtain a SAX based parser to parse XML documents.
SAXParserFactory spf = SAXParserFactory.newInstance();//obtain a new instance of a SAXParserFactory
SAXParser sp = spf.newSAXParser();//creates a new instance of a SAXParser using the currently configured factory parameters.
XMLReader xr = sp.getXMLReader();//interface for reading an xml document using callbacks
LoginHandler myLoginHandler = new LoginHandler();
xr.setContentHandler(myLoginHandler);
xr.parse(retrieveInputStream(responseEntity));//retrieves the response of the server
ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) {
// Store the username and password in SharedPreferences after the successful login
SharedPreferences.Editor editor=mPreferences.edit();
editor.putString("UserName", username1);
editor.putString("PassWord", password1);
editor.commit();
Message myMessage=new Message();
myMessage.obj="SUCCESS";
handler.sendMessage(myMessage);
} else if(parsedLoginDataSet.getExtractedString().equals("ERROR")) {
Intent intent = new Intent(getApplicationContext(), LoginError.class);
intent.putExtra("LoginMessage", parsedLoginDataSet.getMessage());
startActivity(intent);
removeDialog(0);
}
} catch (Exception e)
{
/** InetAddress xxxx;//this code returns the localhost
try {//this code returns the localhost
xxxx = InetAddress.getLocalHost()to.String();*///this code returns the localhost
Intent intent = new Intent(getApplicationContext(), LoginError.class);//calls the activity LoginError.java
intent.putExtra("LoginMessage", "Unable to login");//sends information with intent.putExtra. The information that will be sent is the text which we would like to appear in the TextView of the LoginError activity.(putextra(name, value????))
startActivity(intent);//starts the LoginError.java activity.
removeDialog(0);//closes the dialog box with the message "please wait while connecting...
// e.printStackTrace();//It's a method of the Throwable class. All exceptions are a subclass of that class. The trace prints exactly where the program was at the moment the exception was throw.
/**} catch (UnknownHostException e1) {//this code returns the localhost
e1.printStackTrace();//this code returns the localhost
}*///this code returns the localhost
}
}
it seems to work till the line xr.setContentHandler(myLoginHandler); for the rest of the code I have to build the server. How the server gets the request from the client and then send the response back? Also in the HTTPpost which is the address i have to put? When i put my ip address it does't work but when i use the default geteway it seems to work. At least, can i have the client and the server in the same computer to test the communication or i must have two computers(one for the client and one for the server)?
A lot of questions, i know, but please answer to me at least in some of them. I really am a newbie.
Thank you in advance!