データベースフィールドは
電話番号|| 電話番号 ||
|| 999999999を数える||
1
777777777 || 2
電話番号を入力すると、以下の phone_no に従ってカウントが表示されるように phone_no アクティビティを作成しました。
私のコードは次のとおりです
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/phoneno.php");
String pho = input.getText().toString();
List<NameValuePair> nameValuePairsphedit = new ArrayList<NameValuePair>(11);
nameValuePairsphedit.add(new BasicNameValuePair("phone_no",pho));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsphedit));
Log.d("myapp", "works till here. 2");
try {
HttpResponse response = httpclient.execute(httppost);
Log.d("myapp", "response " + response.getEntity());
String result = EntityUtils.toString(response.getEntity());
Log.d("","Response :"+result);
JSONObject json=new JSONObject(result);
count1 = json.getString("count");//count1 is local variable
}
catch (Exception e)
{
e.printStackTrace();
}
}
私のphoneno.phpファイルは次のとおりです
<?php
$conn= mysql_connect('localhost','root','');
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test',$conn);
$phone_no = mysql_escape_string($_REQUEST['phone_no']);
$sql="SELECT * FROM meh WHERE phone_no='$phone_no'";
$result = mysql_query($sql) or die(mysql_error());
if (!$result)
{
die('There was a problem executing the query');
}
else
{
$rs=mysql_fetch_array($result);
$arrInfo = array('phone_no' => $rs['phone_no'] , 'count' => $rs['count']);
echo json_encode($arrInfo);
}
?>