1

I am working on an app which requires user login and password. My php script is running perfect. But there is some problem with my Android code. On Logging I Came across a stack trace. here is my code

public class MainActivity extends AppCompatActivity
{
EditText userName;
EditText password;
Button sButton;
HttpClient httpClient;
HttpPost httpPost;
HttpResponse httpResponse;
String username;
String pass;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    userName = (EditText)findViewById(R.id.user_id);
    password = (EditText) findViewById(R.id.user_password);
    sButton= (Button) findViewById(R.id.s_button);
    username = userName.getText().toString();
    pass = password.getText().toString();
    httpClient = new DefaultHttpClient();

    httpPost = new HttpPost("http://192.168.100.106/EMS/functions.php");
    final JSONObject jsonObject = new JSONObject();


    sButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
                Thread thread = new Thread()
                {
                    @Override
                    public void run()
                    {
                        try

                        {
                            jsonObject.put("username", username);
                            jsonObject.put("password", pass);
                            Log.wtf("Sent data :","username and password");
                            httpPost.setEntity(new StringEntity(jsonObject.toString()));
                        }

                        catch(JSONException | UnsupportedEncodingException e)

                        {
                            e.printStackTrace();
                        }
                        try {
                            httpResponse = httpClient.execute(httpPost);
                            Log.wtf("Request sent: "," httpresponse");
                            String str = EntityUtils.toString(httpResponse.getEntity());
                            Log.wtf("String recieved ",str);
                            JSONObject responseObject = new JSONObject(str);
                            String response = responseObject.getString("success");
                            Log.wtf("Response recieved ",response);
                            if(response.equals("1"))
                            {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getApplicationContext(), "Credentials match successful.",Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(MainActivity.this,index.class);
                                        startActivity(intent);

                                    }
                                });
                            }

                        } catch (IOException | JSONException e) {
                            e.printStackTrace();
                        }

                    }
                };thread.start();



        }
    });
}

}

And here is my stackTrace

08-08 23:31:46.726  17362-17472/milind.com.ems A/Sent data :﹕ username and password
08-08 23:31:46.771  17362-17472/milind.com.ems A/Request sent:﹕ httpresponse
08-08 23:31:46.811  17362-17472/milind.com.ems A/String recieved﹕ [ 08-08     23:31:46.812 17362:17472 W/System.err ]
org.json.JSONException: End of input at character 2 of

My Php Script is:

<?php

class functions  
{
private $con;

function __construct() 
{
    require_once 'DB_Connect.php';
    $this->db = new DB_Connect();
    $this->con =$this->db->connect();
}
function __destruct() 
{

}

function getUser()
{
    $json= file_get_contents("php://input");
    $str = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($json));
    $str = json_decode($str,true);
    $ID = $str['username'];
    $password = $str['password'];
    $password = $this->clearstring($password);
    // echo "Password :- " . $password;
    $q="SELECT * FROM employers WHERE Employers_ID = '{$ID}' ";        
    $user =$this->con->query($q);        
    //echo mysqli_error($this->con);

    if($user->num_rows >0)
    {
        $row = $user->fetch_assoc();           
        $db_password = $row['Password'];            
        $this->compare($db_password,$password);
    }

   }
    function clearstring($str)
    {
        //$str = strip_tags($str);
        $str = stripcslashes($str);
        $str = htmlspecialchars($str);
        $str = trim($str);
    return $str;

    }
    function compare($db_str, $app_str)
    {   
       // $salt = "ol2ujh354238095uwef";
        $app_str = $app_str/*.$salt*/;
       //echo "<br>Database password:- ".$db_str;
       // echo "<br>Database password:- ".md5($app_str);             
        if(md5($app_str)==$db_str)
        {
            $response['success'] = '1';
        }
        else
        {
            $response['success'] = '0';
        }
        //echo json_encode($response);
        //$response = json_encode($response);
        die(json_encode($response));
        //mysqli_close($con);
    }            
}
  $func = new functions();
  $func->getUser();

?>

2 Answers 2

1
username = userName.getText().toString();
pass = password.getText().toString();

You should be doing this on your onClick method, and not on onCreate.

Sign up to request clarification or add additional context in comments.

Comments

0

Well you are receiving this string:

[ 08-08     23:31:46.812 17362:17472 W/System.err ]

From the server, which is not a valid JSON. This line:

JSONObject responseObject = new JSONObject(str);

tries to parse that string as a json and fails.

I think you are expecting a JSON response from your server always, so I doubt that your this sentence:

My php script is running perfect.

is correct. You should check why your PHP server is responding with that non-JSON response.

1 Comment

Let me post my php script as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.