php - Android 到 JSON 到 PHP 到 MySQL 单一布局 View

标签 php android mysql json

我已经开发一个应用程序大约两周了,我已经能够使用 php 和 mysql 连接并从 json 中提取数据。现在看来我犯了某种类型的错误,它一直告诉我“女巫中缺少必需的字段”是我的 php 中的 if 语句之一,如果我访问我使用字符串 get 通过网络通过 web 访问 php 页面,我尝试在 Android 中对它进行硬编码,这样我至少可以测试它是否可以提取数据,但我不断收到相同的消息错误,任何帮助将不胜感激。谢谢。

ViewListingDetails.java:

package com.listing.spot.listings;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.listing.spot.R;

public class ViewListingDetails  extends Activity implements OnClickListener{



            String id_rs;
            String textTo;
            TextView txtID;
            TextView txtDetails;


            // Progress Dialog
            private ProgressDialog pDialog;

            // JSON parser class
            JSONParser jsonParser = new JSONParser();

            // single product url
            private static final String url_product_detials = "http://adsinsac.com/realtors/droid/listings/homes/listing_details.php";

            // JSON Node names
            private static final String TAG_SUCCESS = "success";
            private static final String TAG_TABLE = "listing";

            private static final String TAG_ID = "id_rs";
            private static final String TAG_DISCRIPTION = "description_rs";


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.view_listing_details);

            // Getting complete product details in background thread
            new GetProductDetails().execute();

            // getting product details from intent
            Intent i = getIntent();

            // getting product id (pid) from intent
            id_rs = i.getStringExtra(TAG_ID);

        }

        /**
         * Background Async Task to Get complete product details
         * */
        class GetProductDetails extends AsyncTask<String, String, String> {

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(ViewListingDetails.this);
                pDialog.setMessage("Loading Listing Details. Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            /**
             * Getting product details in background thread
             * */
            protected String doInBackground(String... params) {

                // updating UI from Background Thread
                runOnUiThread(new Runnable() {
                    public void run() {
                        // Check for success tag
                        int success;
                        try {
                            // Building Parameters
                            List<NameValuePair> params = new ArrayList<NameValuePair>();


                            String HT ;

                            HT= "5004";

                            params.add(new BasicNameValuePair("id_rs", HT));

                            // getting product details by making HTTP request
                            // Note that product details url will use GET request
                            JSONObject json = jsonParser.makeHttpRequest(
                                    url_product_detials, "GET", params);

                            // check your log for json response
                            Log.d("Single Product Details", json.toString());

                            // json success tag
                            success = json.getInt(TAG_SUCCESS);
                            if (success == 1) {
                                // successfully received product details
                                JSONArray productObj = json
                                        .getJSONArray(TAG_TABLE); // JSON Array

                                // get first product object from JSON Array
                                JSONObject product = productObj.getJSONObject(0);


                                // product with this pid found
                                // Edit Text

                                txtID = (TextView) findViewById(R.id.txtDetailID);
                                txtDetails = (TextView) findViewById(R.id.txtDetails);
                                //txtPrayBody = (TextView) findViewById(R.id.txtPrayRequest);
                                /*
                                txtPrayFrom = (TextView) findViewById(R.id.txtYourName);
                                txtPrayerMade = (TextView)findViewById(R.id.txt_num_prayers);
                                */

                                // display product data in EditText
                                txtID.setText(product.getString(TAG_ID));
                                txtDetails.setText(product.getString(TAG_DISCRIPTION));
                                /*
                                txtPrayBody.setText(product.getString(TAG_BODY));
                                txtPrayFrom.setText(product.getString(TAG_FOOTER));
                                txtPrayerMade.setText(product.getString(TAG_NUM_PRAYERS));
                                */

                            }else{
                                // product with pid not found
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

                return null;
            }


            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) {
                // dismiss the dialog once got all details
                pDialog.dismiss();
            }
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }

}

JSON:

{
success: 1,
listing: [
{
id_rs: "5000",
user_id_rs: "1",
retype_rs: "Residential",
price_rs: "515000",
city_rs: "Folsom",
state_rs: "CA",
country_rs: "United States",
description_rs: "GORGEOUS SINGLE STORY NEAR WILLOW SPRINGS RESERVOIR! SALT WATER, PEBBLE TECH POOL W/WATER FEATURE*PRIVATE BACKYARD*LOGGIA*SPACIOUS PATIO & GRASS AREA*BEAUTIFUL KITCHEN OPEN TO FAMILY ROOM W/FIREPLACE*MAPLE CABS*GRANITE COUNTERS*SS GE PROFILE APPLIANCES*TRAVERTINE & HANDSCRAPED WALNUT FLRS*PLANTATION SHUTTERS*SILHOUETTE BLINDS*CROWN MOLDING*CEILING FANS*UPGRADED & RECESSED LIGHTS* BATH SPORTS VESSEL SINK & GRANITE COUNTER*STEPS FROM BEAUTIFUL PRESERVE AND WALKING TRAILS*NEAR INTEL & SHOPPING! Read more on REALTOR.com: 1033 Pintail Cir, Folsom, CA 95630 -",
bedrooms_rs: "4",
bathrooms_rs: "2",
builtin_rs: "",
resize_rs: "2125",
contact_name_rs: "Admin",
contact_phone_rs: "(916) 591-8987",
contact_email_rd: "apps4nerds@gmail.com",
contact_website_rd: "",
contact_address_rd: "230 Spring Oak Ct Folsom CA",
cover_image_rs: "http://adsinsac.com/realtors/uploads/l262fbe44-m0x_1402076456.jpg",
pictures_rs: "http://adsinsac.com/realtors/uploads/l262fbe44-m0x_1402076456.jpg::http://adsinsac.com/realtors/uploads/l262fbe44-m1x_1402076459.jpg::http://adsinsac.com/realtors/uploads/l262fbe44-m2x_1402076461.jpg::http://adsinsac.com/realtors/uploads/l262fbe44-m3x_1402076463.jpg::http://adsinsac.com/realtors/uploads/l262fbe44-m4x_1402076469.jpg::http://adsinsac.com/realtors/uploads/l262fbe44-m4x_1402076472.jpg::http://adsinsac.com/realtors/uploads/l262fbe44-m5x_1402076474.jpg::http://adsinsac.com/realtors/uploads/l262fbe44-m6x_1402076477.jpg::http://adsinsac.com/realtors/uploads/l262fbe44-m7x_1402076479.jpg::",
dttm_rs: "2014-06-06 13:39:38",
dttm_modified_rs: "2014-06-06 13:39:38",
apt_rs: "",
postal_rd: "95630",
subtype_rs: "House",
classification_rs: "Sale",
headline_rs: "1033 Pintail Cir",
address_rd: "1033 Pintail Cir",
latitude_rd: "38.651113",
longitude_rd: "-121.146177",
listing_expire_rd: "2014-08-21",
listing_type_rd: "1"
}
]
}

PHP:

 <?php

$response = array();

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

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

// check for post data
if (isset($_GET["id"])) {
    $id = $_GET['id'];

    // get a product from products table
    $result = mysql_query("SELECT * FROM listing WHERE id = $id");

   if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $daftar_rs = array();
            $daftar_rs["id_rs"] = $result["id"];
            $daftar_rs["user_id_rs"] = $result["user_id"];
            $daftar_rs["retype_rs"] = $result["retype"];
            $daftar_rs["price_rs"] = $result["price"];
            $daftar_rs["price_rs"] = $result["price"];
            $daftar_rs["city_rs"] = $result["city"];
            $daftar_rs["state_rs"] = $result["state"];
            $daftar_rs["country_rs"] = $result["country"];     
            $daftar_rs["description_rs"] = $result["description"];  
            $daftar_rs["bedrooms_rs"] = $result["bedrooms"];
            $daftar_rs["bathrooms_rs"] = $result["bathrooms"];  
            $daftar_rs["builtin_rs"] = $result["builtin"];
            $daftar_rs["resize_rs"] = $result["resize"];
            $daftar_rs["contact_name_rs"] = $result["contact_name"];
            $daftar_rs["contact_phone_rs"] = $result["contact_phone"];
            $daftar_rs["contact_email_rd"] = $result["contact_email"];
            $daftar_rs["contact_website_rd"] = $result["contact_website"];
            $daftar_rs["contact_address_rd"] = $result["contact_address"];
            $daftar_rs["contact_address_rd"] = $result["contact_address"];
            $daftar_rs["cover_image_rs"] = $result["cover_image"];
            $daftar_rs["pictures_rs"] = $result["pictures"];
            $daftar_rs["dttm_rs"] = $result["dttm"];
            $daftar_rs["dttm_modified_rs"] = $result["dttm_modified"];
            $daftar_rs["apt_rs"] = $result["apt"];
            $daftar_rs["postal_rd"] = $result["postal"];
            $daftar_rs["apt_rs"] = $result["apt"];
            $daftar_rs["subtype_rs"] = $result["subtype"];
            $daftar_rs["classification_rs"] = $result["classification"];
            $daftar_rs["headline_rs"] = $result["headline"];
            $daftar_rs["address_rd"] = $result["address"];
            $daftar_rs["latitude_rd"] = $result["latitude"];
            $daftar_rs["longitude_rd"] = $result["longitude"];
            $daftar_rs["listing_expire_rd"] = $result["listing_expire"];
            $daftar_rs["listing_type_rd"] = $result["listing_type"];

             // success
            $response["success"] = 1;

            // user node
            $response["listing"] = array();

            array_push($response["listing"], $daftar_rs);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No product found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

最佳答案

PHP 代码需要一个名为 id 的参数,而您正在发送 id_rs

PHP:

if (isset($_GET["id"])) {
  //....
}

安卓:

params.add(new BasicNameValuePair("id_rs", HT));

您需要它们都是 idid_rs 才能接收预期结果。

关于php - Android 到 JSON 到 PHP 到 MySQL 单一布局 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24196493/

相关文章:

php - Sql,知道记录是否存在,在哪个表中

android - Bootstrap 选项卡在较小的设备中重叠

MySQL拆分字符串返回所有行中的所有单词

android - 在处理 MVP 模式时如何使用 Retrofit call.enqueue() 方法(正确的方法)

c# - 哪种设计模式适合您? ASP.NET 与 MySQL

php - 如何将id保存在数据库中,但在页面上显示用户名

php - UTF-8贯穿始终

php - 将 php-memcached 对象传递给扩展方法

php 用一个变量创建一个变量

java.lang.IllegalStateException : Required view 'caption' with ID 2131230791 for field 'caption' was not found