android - 如何从 phonegap android 应用程序访问 SQL 服务器数据库?

标签 android sql-server json eclipse cordova

我对将 SQL Server 数据库与 Android Phonegap 应用程序连接的方式感到困惑。我使用 PHP 代码来查询数据。

这可能吗?

我的应用没有运行。它是空白的。谁能给我一个清楚的例子或示例代码来解决这个问题..

最佳答案

使用 PHP 将 android 连接到服务器是最好的方法...

首先使用名值对

        public void registerUser(String email, String password, String mobile) {
    // Building Parameters

           List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("tag", register_tag));
    params.add(new BasicNameValuePair("keyemail", email));
    params.add(new BasicNameValuePair("keypassword", password));
    params.add(new BasicNameValuePair("keymobile", mobile));

    // getting JSON Object


    JsonParser.makeHttpRequest(registerURL,params);


}

使用JSON发送数据

      public JSONObject makeHttpRequest(String url,
        List<NameValuePair> params) {


    // Making HTTP request
    try {




            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();





    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
      }

    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();
        json = sb.toString();


    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {

        jObj = new JSONObject(json);
        Log.d("Parser", "IN try parse the string to a JSON object");
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

PHP 代码将获取此值并进一步使用

  // include db connect class
  require_once dirname(__FILE__).'/db_connect.php';

  // connecting to db
  $db = new DB_CONNECT();

  $response = array();



  $email = $_POST['keyemail'];
  $password= $_POST['keypassword'];
 $phone=$_POST['keymobile'];
 $tag=$_POST['tag'];



$result=mysql_query("INSERT INTO TableName (register_email,password,mobile)    VALUES('$email','$password', '$phone')");

if ($result) {
    // successfully updated
    $response["success"] = 1;
    $response["message"] = "Data Inserted Successfully.";

    // echoing JSON response
    echo json_encode($response);
} else {
    $response["error_msg"]="Error In Insertion";
}

?>

关于android - 如何从 phonegap android 应用程序访问 SQL 服务器数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19067153/

相关文章:

android - 应用与运行 Marshmallow 的 Nexus7 不兼容

sql-server - 在 SSRS 中,有没有办法在单元格之间复制格式?

sql - 如何根据每天的日常设备使用情况对 id 进行分割

android - json 数组中元素的计数

android - 如何保存有关手机方向的数据?

android - 亚行 logcat Cordova 安卓 4

sql-server - SQL Server LocalDB 错误 ("parent instance invalid")

javascript - 主干错误未捕获类型错误 : Cannot call method 'each' of undefined?

javascript - 访问 JSON 对象中的子对象

android - 我如何在人行横道项目的 manifest.json 中添加 intent-filter 标签?