douliu3831 2014-09-04 11:06
浏览 41
已采纳

php - 解析单个值到android

I try to write a simple php file, which checks, whether a mysql value exists or not.

For this, I need to parse a simple string from json to android.

e.g: when the value exists, the string is "yes" and when it doesnt exists the string is "no".

Meanwhile I have tried a lot of "solutions", but nothing works.

To do that I usually use this:

  $abfrage = "
SELECT
 Something
FROM
 somewhere
 WHERE
 This=$that"

 ;


$link = mysql_query($abfrage) OR die("Error:"+mysql_error());


while($line = mysql_fetch_assoc($link)){
 $new=$line;

  }


print(json_encode($new));

and in Android:

try
    {
        BufferedReader reader = new BufferedReader
            (new InputStreamReader(is,"utf-8"),8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
    {
            sb.append(line + "
");
        }
            is.close();
            result = sb.toString();
        Log.e("pass 2", "connection success ");
}
    catch(Exception e)
    {
    Log.e("Fail 2", e.toString());
}     

try
    {
 System.out.println(result);


         json_data = new JSONObject(result);

         String name=(json_data.getString("something"));

     Log.e("pass 3", "connection success ");
    }
    catch(Exception e)
    {
        Log.e("result:", result.toString());


        }

    }

This works well, an the the value of the String "name" is the value of "something".

But now my question:

Is it possible to save a string in PHP and parse them to android?

This is what i got:

 $abfrage = "SELECT Something FROM somewhere WHERE This = '$that' LIMIT 1";





// Proof whether the value exists or not

$read = mysql_db_query($db, $abfrage);     
 if (mysql_num_rows($read) == 0){

$value="no";
 }
else{
$value="yes";
};

  print json_encode($value);

And in JAVA:

 HttpClient httpclient = new DefaultHttpClient(); 
 HttpPost httppost = new HttpPost("http://localhost/test.php");
 HttpResponse response = httpclient.execute(httppost);



 String str =  EntityUtils.toString(response.getEntity());
 System.out.println(""+str); //prints: yes


  if(str=="yes") System.out.println("ok"); //not called
  if(str=="no") System.out.println("nope"); //not called
  • 写回答

2条回答 默认 最新

  • dow72046 2014-09-04 12:20
    关注

    I faced this problem months before:

    The problem is, that the String str, not only contains the word "yes". It also contains different letters/symbols, which are not displayed in Android/Eclipse. So you have to manually delete these letters by simply calling substring(int start, int end)

    Try this:

    String str =  EntityUtils.toString(response.getEntity());
    
    
        int number= str.length(); 
        System.out.println("str-length"+number); //You will see not the length of 3 (for    yes)
    
        String s2= str.substring(0, 3);  //short the string
        System.out.println("String s2"+s2);
    
       if(s2.equals("yes"))Log.d("Titles", str);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀