5

Follows my fetching class

 public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
    User user;
    GetUserCallback userCallBack;

    public fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
        this.user = user;
        this.userCallBack = userCallBack;
    }

    @Override
    protected User doInBackground(Void... params) {
        ArrayList<NameValuePair> dataToSend = new ArrayList<>();
        dataToSend.add(new BasicNameValuePair("username", user.username));
        dataToSend.add(new BasicNameValuePair("password", user.password));

        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams,
                CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpRequestParams,
                CONNECTION_TIMEOUT);

        HttpClient client = new DefaultHttpClient(httpRequestParams);
        HttpPost post = new HttpPost(SERVER_ADDRESS
                + "FetchUserData.php");

        User returnedUser = null;

        try {
            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            HttpResponse httpResponse = client.execute(post);

            HttpEntity entity = httpResponse.getEntity();
            String result = EntityUtils.toString(entity);
            JSONObject jObject = new JSONObject(result);

           if (jObject.length() != 0){
                Log.v("happened", "2");
                String name = jObject.getString("name");
                int age = jObject.getInt("age");

                returnedUser = new User(name, age, user.username, user.password);
            }



        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnedUser;
    }

    @Override
    protected void onPostExecute(User returnedUser) {
        super.onPostExecute(returnedUser);
        progressDialog.dismiss();
        userCallBack.done(returnedUser);
    }
}

Php fetching file Fetch_User_Data.php

<?php

$con=mysqli_connect("localhost","root","","loginregister");

if (mysqli_connect_errno($con))
{
 echo "Failed to connect to MySQL: " , mysqli_connect_error();
}

if (isset($_POST["password"])) {$password = $_POST

["password"];}else $password = 'bimbomix';
if (isset($_POST["username"])) {$username = $_POST

["username"];}else $username = 'bimbomix';



$statement = mysqli_prepare($con, "SELECT * FROM user WHERE 

username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, 

$password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $user_id, $name, $age, 

$username, $password);

$user = array();

while(mysqli_stmt_fetch($statement)){
    $user['name'] = $name; 
    $user['age'] = $age;
    $user['username'] = $username;
    $user['password']= $password;       
}

echo json_encode($user);


mysqli_close($con);

?>

Hardcoding Test

        User returnedUser=null;
        try {

            post.setEntity(new UrlEncodedFormEntity(dataToSend));

            HttpResponse httpResponse = client.execute(post);

            HttpEntity entity = httpResponse.getEntity();

            String result = EntityUtils.toString(entity);
//until here hardcoding fills correctly returnedUser
            JSONObject jObject = new JSONObject(result);
returnedUser = new User("hello", 42, user.username, user.password);
// this is hardcoded string that let returnedUser ==null
// so perhaps problem is in jObject               

if (jObject.length() != 0){
                Log.v("happened", "2");
                String name = jObject.getString("name");
                int age = jObject.getInt("age");

              //  returnedUser = new User(name, age, user.username, user.password);
            }



        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnedUser;
    }
Domenico Pacecca
  • 295
  • 1
  • 7
  • 14
  • 1
    try adding return returnedUser; before your catch line. – MetaSnarf Sep 09 '15 at 02:41
  • 1
    Try adding an else after your `if (jObject.length() != 0){` and logging the contents of `result` to see what PHP is tossing back otherwise if your `Log.v("happened", "2");` is showing, try adding another Log.v afterwards showing the contents of `result`. – Ultimater Sep 09 '15 at 02:42
  • @ MetaSnarf It gives error because return stmt belongs to doInBackground method – Domenico Pacecca Sep 09 '15 at 02:51
  • where did you get the stmt? – MetaSnarf Sep 09 '15 at 02:53
  • @ Ultimater please how can I see Log.v ? I have copyed that but I'm not very practice of Log – Domenico Pacecca Sep 09 '15 at 02:54
  • @ Ultimater howewer I saw result variable and it gives a strange html page sayng among other lot of things "Cannot found object " or similar – Domenico Pacecca Sep 09 '15 at 03:05
  • 1
    See http://android.stackexchange.com/questions/14430/how-can-i-view-and-examine-the-android-log regarding how to read the logs. Regarding your error, it seems PHP is erroring from whatever you're sending it. I'd suggest in your PHP to use `file_put_contents('globals-sent-to-php.txt',print_r($GLOBALS,true));` somewhere near the top then look for a file in your server's directory with the contents directly after Android makes a request. – Ultimater Sep 09 '15 at 03:12
  • 1
    If your seeing an html response, then you may have an error on your server file OR you may have a wrong params values. Try checking your params for it might contain null or invalid values. – MetaSnarf Sep 09 '15 at 03:13
  • Done it, report: Array ( [_GET] => Array ( ) [_POST] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) [GLOBALS] => Array *RECURSION* ) – Domenico Pacecca Sep 09 '15 at 03:16
  • What happens if you change line one of your doInBackground function to this: `ArrayList dataToSend = new ArrayList();` – Ultimater Sep 09 '15 at 03:28
  • Gives a ClassNotFoundExeption – Domenico Pacecca Sep 09 '15 at 03:34
  • The strange thing is that in registration is all ok the problem is in fetching, I'm using a real device and if you want to see the whole project I have posted it http://stackoverflow.com/questions/32278491/android-http-php-json-mysqli-web-services-insert-and-retrieve-data-doesnt-work – Domenico Pacecca Sep 09 '15 at 03:38
  • What if you hardcode "user.username" and "user.password" with something else? – Ultimater Sep 09 '15 at 03:54
  • Hi Ultimater: I tried hardcoding that in senddata as you suggested but didn't work. I tried hardcoding out of try-catch block and worked – Domenico Pacecca Sep 10 '15 at 00:53
  • I was referring at hardcoding of retunedUser(...) – Domenico Pacecca Sep 10 '15 at 01:08
  • Edit: see last paragraph Hardcoding Test – Domenico Pacecca Sep 10 '15 at 03:15

1 Answers1

0

I'm so sorry. Issue was simple, even if it has gone me mad for a pair of weeks! Look at fetch file "Fetch_User_Data.php" and then look at string where I call it as "FetchUserData.php" and you'll be done...

Domenico Pacecca
  • 295
  • 1
  • 7
  • 14