php - Android从远程数据库获取数据

标签 php android json

我正在制作一个 Android 应用程序,我正在尝试从远程数据库检索数据。

我的问题是如何检查查询结果是否包含数据且不为空?

我正在使用 JSON,但我是新手。这是我的代码:

public class MainActivity extends Activity {

    private TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout rootLayout = new LinearLayout(getApplicationContext());
        txt = new TextView(getApplicationContext());
        rootLayout.addView(txt);
        setContentView(rootLayout);

        txt.setText("Connexion...");
        txt.setText(getServerData(strURL));

    }

    public static final String strURL = "http://.../marecherche/adresse.php";

    private String getServerData(String returnString) {
        InputStream is = null;
        String result = "";

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("adresse","adr casa"));

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(strURL);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }catch(Exception e){

            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        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){
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try{


            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);

                Log.i("log_tag","raison sociale: "+json_data.getString("raison_social")+
                        ", Adresse: "+json_data.getString("adresse")
                        );

                returnString += "\n\t" + jArray.getJSONObject(i);
            }
        }catch(JSONException e){
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
        return returnString;
    }
}

和 php 文件:

<?php
mysql_connect("localhost", "root", "passwd");
mysql_select_db("dbname");
$mots = explode(' ', $_REQUEST['adresse']);
$like = array();
foreach ($mots AS $mot) {
    $like[] = ' "%' . mysql_real_escape_string($mot) . '%" ';
}

$condition = implode(' OR adresse LIKE ', $like);

$sql = mysql_query("SELECT * FROM entreprise WHERE adresse like " . $condition);
while ($row = mysql_fetch_assoc($sql))
    $output[] = $row;
print(json_encode($output));
mysql_close();
?>

最佳答案

您可以创建自己的返回来检查,比如:

PHP 文件:

if (is_array($output)) {
    print(json_encode($output));
} else 
    echo "empty";
}

java :

if (result.equals("empty")) {
    return;
}
JSONArray jArray = new JSONArray(result);
// etc

关于php - Android从远程数据库获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15908424/

相关文章:

php - 将表单中的数据插入表不起作用,不返回错误

android - 免费图标库

android - 调整 Android 多行 TextView 的大小

迭代存储在文本文件中的 json 对象集合的 pythonic 方式

ios - 将数据从 Tableview 传递到 Tableview Detail

php - 如何通过php.ini配置过滤掉通知

php - htmlspecialchars 正在删除字符

php - 按 DESC 查询循环排序

android - "loading"使用 cordova (phonegap) 时自动附加 div

java - 解析具有不同键的 JSON 对象