2

こんにちはみんな私の現在のAndroidアプリにウェブビューがあるアクティビティがあります。そのWebサイトのphpから変数を取得して(はい、Webサイトを制御し、完全な編集機能を備えています)、Androidアプリの変数に格納する方法を考えていました。

4

2 に答える 2

5

一般的にやらなければいけないことを説明します。まず、Androidアプリ側では、次のようなコードが必要です。

1)あなたのアンドロイド側で

String url = "www.yoururl.com/yourphpfile.php";
List<NameValuePair> parmeters = new ArrayList<NameValuePair>();
            parameters.add(new BasicNameValuePair("category",category));
            parameters.add(new BasicNameValuePair("subcategory",subcategory));// These are the namevalue pairs which you may want to send to your php file. Below is the method post used to send these parameters

  DefaultHttpClient httpClient = new DefaultHttpClient();

  HttpPost httpPost = new HttpPost(url); // You can use get method too here if you use get at the php scripting side to receive any values.

  httpPost.setEntity(new UrlEncodedFormEntity(parameters));

  HttpResponse httpResponse = httpClient.execute(httpPost);

  HttpEntity httpEntity = httpResponse.getEntity();

  is = httpEntity.getContent();

   BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8); // From here you can extract the data that you get from your php file..

        StringBuilder builder = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) {
            builder.append(line + "\n");
        }

        is.close();
        json = sb.toString(); // Here you are converting again the string into json object. So that you can get values with the help of keys that you send from the php side.

        String message = json.getString("message");
         int success = json.getInt("success"); // In this way you again convert json object into your original data.

2)あなたのphp側で

$response = array();

// check for post fields
if (isset($_POST['category']) && isset($_POST['subcategory'])) {

    $category = $_POST['category'];
    $subcategory = $_POST['subcategory'];


    // include db connect class
    require_once __DIR__ . '/db_connect.php';

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

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO categorytable(category, subcategory) VALUES('$category', '$subcategory')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Data inserted into database.";

        // echoing JSON response
        echo json_encode($response);// Here you are echoing json response. So in the inputstream you would get the json data. You need to extract it over there and can display according to your requirement.
} 

あなたがphpにかなり熟練しているとあなたが言ったように、あなたはphpの上記のものが可能な限り単純にされ、それが安全ではないことに気づいたかもしれません。したがって、php側でコーディングするためのいくつかのpdoまたは他の安全な方法に従うことができます。必要な場所で別のスレッドで実行するために、Android側のコードをasynctaskで囲んでください。お役に立てれば。

于 2012-11-30T03:42:58.357 に答える
0

Androidアプリは、必要なデータを返すためにPHPスクリプトを設定するURLで、PHPアプリにリクエストを送信する必要があります。

ストレートHTMLを返すことも、データを返してAndroidアプリに許可するだけの場合は、JSONが優れたデータ転送形式です。XMLも人気がありますが、JSONはよりスリムで、最近は十分にサポートされています。

于 2012-11-29T22:18:04.717 に答える