How to retrieve user data from MYSQL? - android

I'm making an android application through Android Studio and I want to retrieve user details from a MYSQL database and display these details in different text views. So what are the possible ways to do the same?
I have attached my php code. I want to use the JSON array data and display it in text views.
<?php
$db_name="society_management";
$mysql_user="root";
$mysql_pass="";
$server_name="localhost";
$mail = $_POST['Email'];
$con=mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
$sql = "select * from member;";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,aaray(
"mid"=>$row[0],
"nm"=>$row[1],
"hsno"=>$row[2],
"sts"=>$row[3],
"email"=>$row[4],
"mno"=>$row[5]
));
}
echo json_encode(array("result"=>$result));
mysql_close($con);
?>

You need to create web-service at server side that must return json response of Sql data you requires.
For calling that service from android code and display to textview can be easily achieved by Retrofit lib. That automatically parse json data to Java Model.

Need to use web services , Here is a good example http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

Create a web-service at the server side which should return data in JSON format.
We can access data from the server by using either of the 3 ways in our android application :
Using Async task
Using Volly library
Using Retrofit

Related

How to fetch data in retrofit 2 using query parameters in your Url?

I have a URL for example www.fetchdata123.com/myservice.php?CityId=5
and want to fetch data on behave of CityId using
#GET("my_service.php?")
Call<List<Employees>> GetEmployees(#Query("CityId") String CityId);
How to get the Employees data using that particular CityId.
You are having en extra ? ,Change your code like this
#GET("my_service")
Call<List<Employees>> GetEmployees(#Query("CityId") String CityId);
while using #Query, ? is automatically appended

Android data need to insert in database dynamically

We are getting the request from Android app as json array. For example:
$var = [{"username":"kp","mailid":"kp#gmail.com","phoneno":"9876543563","groupname":"android"}]';
I want to make this static data (kp, kp#gmail.com, 9876543563, android) as dynamic to insert the values into database.
In what method we are getting the data? And from an android app, is it POST or GET variable, or an other method? Please let me know how to make this value dynamic which is coming from the android app.
How to do you want to send the data from Android App? Is it needed to be namely JSON-format?
I think the most useful case is to use usual, e.g., POST data and retrieve this using base methods to save it inside PHP. You can use, of course, GET format for 'short' request.
For instance,
Android sends using GET: http://yourcompany.com/senddata?username=name&mailid=e#yourmail.com
You PHP code (very-very-very simple approach):
/// ... initialization...
$sql = "INSERT INTO users (username, email) VALUES ('". ($_POST["username"]) . "', '". ($_POST["mailid"]) . "')";
mysqli_query($conn, $sql)
PHP insert example
To create POST or GET request in Android I would recommend to use JSOUP library
Download
A simple example for jsoup:
Document doc = Jsoup.connect("http://yourcompany.com/url?var1=a").get();

How retrieve data from websocket?

Please how i can retrieve data and username from web socket,
io.sockets.emit('update chat', 'username':socket.username, 'data':data);
i'm sending data from android to socket then i want retrieve this data in my server chat but i get [object object],
You may need to use JSON.stringify before emiting the data variable like
io.sockets.emit('update chat'
, 'username':socket.username
, 'data':JSON.stringify(data));
and parse same string back to json form at recieving end via JSON.parse like
var objData = JSON.parse(data);

Ajax post data in android java

I am new t ajax, but quite familiar with android. I am converting a ajax program to android app. As a part of it, i need to post data to the server. Below is the given post command in ajax.
var postTo = 'xyz.php';
$.post(postTo,{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' } ,
function(data) {
if(data.success){
window.localStorage["sa_id"] = data.mid;
window.location="getempdb.html";
}
if(data.message) {
$('#output').html(data.message);
} else {
$('#output').html('Could not connect');
}
},'json');
I want to implement this in android but under very little from the above statements. Could anyone who is good at ajax help me out with this thing. As of now, i get the user name and telephone number as a edit text input. I need to send this to php using http client. I know how to send data using php, but do not know what format to send and whether its a string to send or as a json object to send. Please help in interpreting the above code and oblige.
Apparently, this uses UrlEncodedFormEntity if you are using HttpClient in android.
This is created by using a List of NameValuePair.
from the parameters to the $.post:
{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' }
You have to create a NameValuePair for employee_name, one for phone ... each of which is fetched from a HTML element name employee_name, phone ... This is where you put the values from your EditTexts.
It returns a JSON formatted String, which you have to parse (typically using JSONObject obj = new JSONObject(result); once you have fetched the result from the server)
In this JSON object, you have a key named success, which format is not specified, except you can assume things went well if it is present ; a key mid, and a key message.

How i write in json file on server

i have JSON FILE on my localhost i
want to update it via code how i do that any help plz ?
i have list.JSON ON MY LOCAL
HOST i want delete all data in it and
write new data in it
and update it on server ?
can any one tell me how i do that ?
///////////////////////////////////////////////
//////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////
///////////////////////////////////////
//////////////////////////////////
///////////////////////////
////////////////////
How to send a JSON object over Request with Android?
1- crate a page on server which takes this json and update it and take it's url
2- take the input from user
3- put it in a bean/model (wrap in object form as per structure )
4- crate the json from object (can use libraries like GSON for this
5- use post method to push json to srver

Categories

Resources