Android Json值没有返回

时间:2013-11-27 06:20:13

标签: java php android json

我正在开发一个Android应用程序,我在其中使用JSon从数据库中获取值。在数据库中,每个字符串A-Z都有特定的文本。当从A-Z返回一个字符串时,必须显示相应的结果......我正在给出代码...但是问题结果不显示..我使用PHP代码进行同步...

PHP

    $response = array();
    // include db connect class
    require_once __DIR__ . '/db_connect.php';
    // connecting to db
    $db = new DB_CONNECT();
    // check for post data
    if (isset($_GET["costone"])) 

    {
        $costone = $_GET['costone'];
        // get a product from products table
        $result = mysql_query("SELECT *FROM sletter WHERE costone = $costone");

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

                $result = mysql_fetch_array($result);

                $product = array();
                $product["pid"] = $result["pid"];
                $product["costone"] = $result["costone"];
                $product["custone"] = $result["custone"];
                $product["fvowel"] = $result["fvowel"];
                $product["cornerstone"] = $result["cornerstone"];
                $product["cupstone"] = $result["cupstone"];
                $product["firstvowel"] = $result["firstvowel"];

                $product["created_at"] = $result["created_at"];
                $product["updated_at"] = $result["updated_at"];


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

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

                array_push($response["product"], $product);

                // 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);
    }
    ?>

活动

String custone, costone;
TextView txtName9, txtNzme10;
// Progress Dialog
private ProgressDialog pDialog;

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


private static final String url_cup_stone = "http://iascpl.com/app/get_cup_stone.php";
private static final String url_first_vowel = "http://iascpl.com/app/get_first_vowel.php";

private static final String TAG_CUPSTONE = "cupstone";
private static final String TAG_FIRSTVOWEL = "firstvowel";

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

    txt27.setText(getIntent().getStringExtra("aChar"));
    TextView txt28 = (TextView) findViewById(R.id.textView51);
    txt28.setText(getIntent().getStringExtra("aChar1"));


    costone = txt27.getText().toString();
    custone = txt28.getText().toString();


    /**
     * 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(FullExplanation.this);
            pDialog.setMessage("Loading the result... Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

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

        {

            TextView txt27 = (TextView) findViewById(R.id.textView48);
            TextView txt28 = (TextView) findViewById(R.id.textView51);

            costone = txt27.getText().toString();
            custone = txt28.getText().toString();

            params.add(new BasicNameValuePair("costone", costone));
            params.add(new BasicNameValuePair("custone", custone));

            JSONObject json9 = jsonParser.makeHttpRequest(
                    url_corner_stone, "GET", params);

            JSONObject json10 = jsonParser.makeHttpRequest(
                    url_cup_stone, "GET", params);

            int success9 = json9.getInt(TAG_SUCCESS);
            int success10 = json10.getInt(TAG_SUCCESS);

            if (success9 == 1) {
                // successfully received product details
                JSONArray productObj = json9
                        .getJSONArray(TAG_PRODUCT); // JSON Array

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

                // product with this pid found
                // Edit Text

                txtName9 = (TextView) findViewById(R.id.textView49);


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        try {

                            txtName9.setText(product.getString(TAG_CORNERSTONE));
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });

            } else {
                // product with pid not found
            }

            if (success10 == 1) {
                // successfully received product details
                JSONArray productObj = json10
                        .getJSONArray(TAG_PRODUCT); // JSON Array

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

                // product with this pid found
                // Edit Text

                txtName10 = (TextView) findViewById(R.id.textView52);


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        try {

                            txtName10.setText(product.getString(TAG_CUPSTONE));
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });

            } 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();
}

}

0 个答案:

没有答案