February 6th, 2012, 12:00 PM
|
#7 (permalink)
|
|
Senior Member
Join Date: Jul 2010
Posts: 977
Device(s): Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Thanks: 52
Thanked 199 Times in 144 Posts
|
your PHP isn't setup for JSON. change it to something like this:
Code:
$un=$_POST['login'];
$pw=$_POST['password'];
//connect to the db
$user = 'root';
$pswd = '';
$db = 'xxx';
$conn = mysql_connect('localhost', $user, $pswd);
mysql_select_db($db, $conn);
//run the query to search for the username and password the match
$query = "SELECT firstname,lastname FROM customers WHERE login = '$un' AND password = '$pw' OR usertype ='A'";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
while($e=mysql_fetch_assoc($result))
$output[]=$e;
print(json_encode($output));
mysql_close();
after that, in your android application, you would need to do something like so:
Code:
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("login", yourLoginStringHere));
nameValuePairs.add(new BasicNameValuePair("password", yourPasswordStringHere));
try
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(ROTM_GET_URL);
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(IOException e)
{
//Handle your exception here
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
sb.append(line + "\n");
is.close();
result = sb.toString();
}
catch(IOException e)
{
//Handle exception here
}
try
{
JSONArray jArray = new JSONArray(result);
for(int i = 0; i < jArray.length(); i++)
{
JSONObject json_data = jArray.getJSONObject(i);
//Do whatever you need with the data here.
}
}
catch(Exception e)
{
//Handle excpetions here
}
|
|
|