Skip to navigation Skip to content
Simon Technology Blog
  • Architecture
  • Cloud
  • Database
  • Develop
  • Hardware
  • Industry
  • Language
  • Mobile
  • Opensource
  • OS
  • Web
Main Navigation

Even if the PHP file in the browser chrome is normal, the JSON object is empty when Android is logged in.

October 11, 2021By Simo Browser

Follow my course

public class fetchUserDataAsyncTask extends AsyncTask {
User user;
GetUserCallback userCallBack;

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

@Override
protected User doInBackground(Void... params) {
ArrayList 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);

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

User returnedUser = null;
< br /> 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 gets the file Fetch_User_Data.php

< pre>{
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($st atement);
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);

?>

Hard coded 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;
}
I’m sorry. The problem is simple, even if it drives me crazy for a week! Check the fetch file “Fetch_User_Data.php”, then check the string, I will call it “FetchUserData.php”, you will complete…

Follow my course

public class fetchUserDataAsyncTask extends AsyncTask {
User user;
GetUserCallback userCallBack;

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

@Override
protected User doInBackground(Void ... params) {
ArrayList 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);
< br /> 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) {< br /> super.onPostExecute(returnedUser);
progressDialog.dismiss();
userCallBack.done(returnedUser);
}
}

Get Php File Fetch_User_Data.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);

?>

Hard coded 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;
}

I’m sorry. The problem is simple, even if it drives me crazy for a week! Check the fetch file “Fetch_User_Data.php”, then check the string, I will call it “FetchUserData.php”, you will be done…

Android, browser, Chrome, empty, Even, execution, file, JSON, login, Normal, object, PHP, time

Post navigation

Express – Detecting movement in a group reaction
IE6 CSS question on the link

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4316 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment Cancel reply

Your email address will not be published.

Recent Posts
  • Sencha-Touch-2 – Sencha Touch 2, Nested XML Analysis NodeValue
  • Add a separation line and format XML content
  • Is there a norm of simplified XML subsets?
  • Look at it when you write React
  • ReactJS – Present React Redux React-Router App to add the server to the Firebase hosted by the Firebase
Categories
  • Android
  • Apache
  • Apache Kafka
  • Asp
  • Auto-Test
  • Automated Build
  • Aws
  • Bitcoin
  • Browser
  • C & C++
  • C#
  • Centos
  • Cgi
  • Character
  • Cloud Service
  • Cocos2dx
  • Cordova
  • CSS
  • Data Structure
  • Delphi
  • Design Pattern
  • Dojo
  • Dubbo
  • ELK
  • Flex
  • football
  • Game
  • Hadoop
  • Hibernate
  • HTML
  • Hybrid
  • Intel
  • IOS
  • Ipad
  • iPhone
  • Java
  • Javascript
  • Jetty
  • JQuery
  • Jsp
  • Linux
  • Load Balance
  • Lua
  • Macbook
  • Macos
  • Mathematics
  • Micro Services
  • Monitoring
  • Motherboard
  • Mysql
  • Network Hardware
  • Network Marketing
  • Nginx
  • NodeJs
  • Nosql
  • Oracle
  • Os Theory
  • Performance
  • PHP
  • Postgresql
  • Power Designer
  • React
  • Redis
  • Regexp
  • Rom
  • Rss
  • Ruby
  • Search Engines
  • Shell Script
  • Silicon Valley
  • Silverlight
  • Software Design
  • Spring
  • Sql
  • Sqlite
  • Sqlserver
  • Storage
  • Storm
  • Surface
  • SVN
  • Swift
  • System Architecture
  • Tablet
  • Uncategorized
  • Unix
  • Visual Basic
  • Visual Studio
  • Web Crawler
  • WebService
  • Windows
  • Wireless
  • XML
  • ZooKeeper
Archives
  • October 2021
  • September 2021
  • August 2021
  • May 2021
  • April 2021
  • September 2020
  • September 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
© Simon Technology Blog 2025 • ThemeCountry Powered by WordPress