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.

Recommended Answers

All 3 Replies

You sure it is Android app and not your website that is problematic? I got this error when visited login

Warning: include(spi/connect.php): failed to open stream: No such file or directory in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 4 Warning: include(spi/connect.php): failed to open stream: No such file or directory in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 4 Warning: include(): Failed opening 'spi/connect.php' for inclusion (include_path='.;C:\php\pear') in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 4 Notice: Undefined index: username in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 6 Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 6 Warning: mysql_real_escape_string(): A link to the server could not be established in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 6 Notice: Undefined index: password in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 7 Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 7 Warning: mysql_real_escape_string(): A link to the server could not be established in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 7 Warning: mysql_query(): Access denied for user ''@'localhost' (using password: NO) in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 9 Warning: mysql_query(): A link to the server could not be established in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 9 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\zpanel\hostdata\djmann1013\public_html\login.php on line 12

@peter_budo it still does not work after I fixed the login.php

I will try to have look at it later, just returned from holidays...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.