Hi, my app that I am making is not working. Here is the code:
package com.awsomechat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText etUser, etPass;
Button blogin;
// Username, Password
String username, password;
// Make an HTTP Client
HttpClient httpclient;
HttpPost httppost;
// Store the username and password in an array
ArrayList<NameValuePair> nameValuePairs;
// HTTP Response & Entity
HttpResponse response;
HttpEntity entity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialise();
}
private void initialise() {
// TODO Auto-generated method stub
etUser = (EditText) findViewById(R.id.etUser);
etPass = (EditText) findViewById(R.id.etPass);
blogin = (Button) findViewById(R.id.etSubmit);
blogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// The user tapped the Login button, start logging in
// Default HTTPClient
httpclient = new DefaultHttpClient();
// Post the values to the AwsomeChat Login script
httppost = new HttpPost("http://beta-awsomechat.tk/login.php");
// THe values we are working with
username = etUser.getText().toString();
password = etPass.getText().toString();
// Try to login. Start the login validation/process
try {
nameValuePairs = new ArrayList<NameValuePair>();
// Store the username and password in an array
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode() == 200){
// Get the info given to us.
entity = response.getEntity();
if(entity != null){
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
String retUser = jsonResponse.getString("username");
String retPass = jsonResponse.getString("password");
// Start the validation process
if(username.equals(retUser) && password.equals(retPass)){
//
//
SharedPreferences sp = getSharedPreferences("logindetails", 0);
SharedPreferences.Editor spedit = sp.edit();
spedit.putString("user", username);
spedit.putString("pass", password);
//
spedit.commit();
Toast.makeText(getBaseContext(), "Login Success", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "Login Fialed.", Toast.LENGTH_SHORT).show();
}
}
}
} catch(Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Login Failed.", Toast.LENGTH_SHORT).show();
}
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
I am not sure what is making this app not work. I don't know how to fix this. The wierd thing is is that LogCat did not display anything, nor the Console. I really don't know what is wrong here.