如何在android中显示外部数据库内容并将其分配给局部变量

如何在android中显示外部数据库内容并将其分配给局部变量,android,Android,数据库字段是 电话号码|计数 999999999 | | 1 777||2 我已经创建了phone_no活动,因此如果我输入电话号码,它将根据下面的phone_no显示计数 我的代码如下 { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/phoneno.php"); String pho = input.getText().toStrin

数据库字段是

电话号码|计数
999999999 | | 1
777||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();
} 
                    }
<?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);

    }
?>
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2/phoneno.php");
字符串pho=input.getText().toString();
列表名称ValuePairsphedit=新的ArrayList(11);
nameValuePairsphedit.add(新的BasicNameValuePair(“电话号码”,pho));
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairsphedit));
Log.d(“myapp”,“工作到这里2”);
试一试{
HttpResponse response=httpclient.execute(httppost);
Log.d(“myapp”、“response”+response.getEntity());
字符串结果=EntityUtils.toString(response.getEntity());
Log.d(“,”响应:“+结果);
JSONObject json=新的JSONObject(结果);
count1=json.getString(“count”);//count1是局部变量
} 
捕获(例外e)
{
e、 printStackTrace();
} 
}
我的phoneno.php文件如下

{
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();
} 
                    }
<?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);

    }
?>

使用下面的方法。您正在尝试直接读取response.getEntity()。您需要首先从该实体获取InputStream,然后从该InputStream读取服务器流内容

public static JSONObject read(String url)  {

    InputStream is = null;
    String result = "";
    JSONObject jsonObject = null;
    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("phone_no",pho));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        e.printStackTrace();
    }

    // convert response to string
    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 (Exception e) {
        e.printStackTrace();
    }

    //convert the read response to JSONObject
    try {
        System.out.println("Read data from server :" + result+":");
        jsonObject = new JSONObject(result);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return jsonObject;
}
公共静态JSONObject读取(字符串url){
InputStream=null;
字符串结果=”;
JSONObject JSONObject=null;
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“电话号码”,pho));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e、 printStackTrace();
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
e、 printStackTrace();
}
//将读取响应转换为JSONObject
试一试{
System.out.println(“从服务器读取数据:“+result+”:”;
jsonObject=新的jsonObject(结果);
}捕获(JSONException e){
e、 printStackTrace();
}
返回jsonObject;
}

日志语句的输出是什么?我试图打印count1值,但它显示为空感谢重播,但问题是它显示“从服务器读取数据:”没有其他我称该方法为读取(“url”);这是正确的方法吗?请建议,我只需要显示count value而不是其他任何内容。这是使用代码段的正确方法。但是,您必须为PHP Web服务传递有效的“phone_no”参数才能返回值。如果可能,请通过一些核心java示例检查PHP服务是否正常工作。请参阅上面的PHP code并告诉我这是将值传递给android代码的方法我只需要打印计数值就可以了,请它看起来很简单,但我从过去几天就开始尝试。我不是一个PHP的家伙。但是看看你的PHP代码,你从android客户端传递的phone\u no参数和你在PHP中收到的参数之间似乎存在冲突代码。即,您从android传递'phone_no'并尝试在PHP中读取'phone'参数。我注意到的另一个错误是,您将post参数读取到一个局部变量'phone',然后在sql查询中尝试传递'phone_no'变量。这也会导致PHP代码中出现问题,请更改$phone=mysql_escape_字符串($\u REQUEST['phone');$sql=“选择*FROM-meh WHERE-phone\u-no='$phone\u-no'”;**到**$phone=mysql\u-escape\u字符串($\u-REQUEST['phone\u-no']);$sql=“SELECT*FROM-meh-WHERE-phone\u-no='$phone'”;我希望这能奏效