Android:Send data from java to a php file and have a response
I'm programming an android app and I have a log in activity where the user must insert a UserName and Password so I need to connect my app with my database to check if the user is registred.
I've created a mysql database on Altervista and also a php file which performs a query. This is the code of login.php:
<?php
$UserName =$_POST['name'];
$Password =$_POST['pass'];
$hostname="localhost";
$user="myuser";
$pass="mypass";
$conn=mysql_connect($hostname,$user,$pass);
if(!$conn){
echo("Error");
}
$db=mysql_select_db('my_finditdatabase');
if(!$db)
{
echo "db non presente o mancata selezione";
}
$query="SELECT UserName,Password FROM Utente WHERE UserName='$UserName' AND Password='$Password';";
$ris=mysql_query($query);
$cont=0;
$riga=mysql_fetch_array($ris);
while($riga)
{
print(json_encode("Exist"));
$cont++;
$riga=mysql_fetch_array($ris);
}
if($cont==0){
print(json_encode("NoExist"));
echo mysql_error();
}
mysql_close();
?>
I need to connect to the url,send from java the values of username and password
$UserName =$_POST['name'];
$Password =$_POST['pass'];
and then receive from php file the print 'Exist' or 'NoExist'.
print(json_encode("Exist"));
print(json_encode("NoExist"));
I've already tried codes from Internet and they gives me some error on lot of deprecated methods like: DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se; se = new StringEntity(jsonObjSend.toString());
and others...
How can i do ? please help me because I really need it. Thanks
Good Evening Jay, First let's build the JSON and send to server, in Android APP create an AsyncTask or some other Asynchronous service to do the web connection.
Then do as follow example:
HttpURLConnection urlConnection = null; // Will do the connection
BufferedReader reader; // Will receive the data from web
String strJsonOut; // JSON to send to server
Uri buildUri = Uri.parse("http://url.com.br").buildUpon().build(); // build the uri
//inside a try to prevent errors
try {
URL url = new URL(buildUri.toString().trim());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(0);
urlConnection.setConnectTimeout(1500); // timeout of connection
urlConnection.setRequestProperty("Content-Type", "application/json"); // what format will you send
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true); // will receive
urlConnection.setDoOutput(true); // will send
urlConnection.connect();
Stream outputStream = urlConnection.getOutputStream();
OutputStreamWriter ow = new OutputStreamWriter(outputStream);
JSONObject objLogin = new JSONObject();
objLogin.put("pass",passVariable);
objLogin.put("name",nameVariable);
ow.write(objLogin.toString());
ow.close();
//information sent by server
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// response is empty, do someting
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String strJsonServer = buffer.toString();
objServerResponse = new JSONObject(strJsonServer);
boolean status = objServerResponse.getBoolean("status");
return status; // Your response, do what you want.
}catch (JSONException e, IOException er)
{
// error on the conversion or connection
}
On the server, just create an array with the status key.
$name = $_POST['name'];
$pass = $_POST['pass'];
// do your logical
$response = array();
if(true){
$response['status'] = 'true';
}
else{
$response['status']= 'false';
}
echo json_encode($response,JSON_UNESCAPED_UNICODE); // Second parameter to correct special characters to UTF-8
I'm not able to test this solution at moment, if have some error, check if my syntax is correct and prevent possibles errors on the catch clause.
链接地址: http://www.djcxy.com/p/29594.html