1

私は000webhostでPHPをサポートする非常に豊富なデータベースmySQLを備えたアプリケーションを作成しようとしています。私たちはその人の気分を計算しようとしています。

Color_IDElement_IDに基づいてスコアを選択するColorElementの2つのテーブルがあります。実際、私はandroid ApplicationによってユーザーからColour_IDElement_IDの値を取得し、それをJSONPARSINGに渡しました。

mood.phpのコードは次のとおりです。

<?php
$response = array();

if (isset($_POST['Colour']) && isset($_POST['Element'])) { 

$colour = $_POST['Colour'];
$element = $_POST['Element'];  

$con = mysql_connect("mysql1.000webhost.com","a4633783_snyz","software123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("a4633783_snyz", $con);

// get color id from color table

$result_colour = mysql_query("SELECT Score FROM COLOUR WHERE Colour_ID = $colour");

$result1 = mysql_fetch_array($result_colour);

$result_element = mysql_query("SELECT Score FROM ELEMENT WHERE Element_ID = $element");

$result2 = mysql_fetch_array($result_element);

$main_result = ($result1[0] + $result2[0]);

$mood=mysql_query("SELECT Mood FROM CHECK_MOOD WHERE Score = $main_result");

$row = mysql_fetch_array($mood);

$response["mood"]=array();
array_push($response["mood"], $row);

// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No mood available";
// echo no users JSON
echo json_encode($response);
}
?>

今私が欲しいのは、ユーザーがColour_IDとして1を選択し、 Element_IDとして1を選択すると、このAndroidアプリケーションでスコア2のムードが表示されることです。

Mood.javaは次のとおりです。

public class mood extends Activity {

private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();

RadioGroup color;
RadioGroup element;
TextView JsonResult;
String mood_result;

// url to create new product
private static String url_create_signup = "http://snyz.site88.net/mood.php";

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mood);
    color = (RadioGroup) findViewById(R.id.color);
    element = (RadioGroup) findViewById(R.id.element);
    JsonResult = (TextView) findViewById(R.id.json_result);

    // Create button
    Button btnCreateProduct = (Button) findViewById(R.id.submit);

    // button click event
    btnCreateProduct.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            new signup().execute();
        }
    });
}

/**
 * Background Async Task to Create new product
 * */
class signup extends AsyncTask<String, String, String> {

    int color_value = color.getCheckedRadioButtonId();
    int element_value = element.getCheckedRadioButtonId();

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

    protected String doInBackground(String... args) {
        String Colour = Integer.toString(color_value);
        String Element = Integer.toString(color_value);

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("Colour", Colour));
        params.add(new BasicNameValuePair("Element", Element));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_signup,
                "POST", params);

        // check log cat for response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {

                JSONArray jArray = json.getJSONArray("ARRAYNAME");

                // for (int i = 0; i < jArray.length(); i++) {
                JSONObject oneObject = jArray.getJSONObject(0);
                // Pulling items from the array
                mood_result = oneObject.getString("STRINGNAMEinTHEarray");
                // String oneObjectsItem2 = oneObject
                // .getString("anotherSTRINGNAMEINtheARRAY");

                String JsonResultt = "\n\nYour mood is: " + mood_result;

                JsonResult.setText("\n" + JsonResultt);

                // display.show();
                // successfully created product
                // Intent i = new Intent(getApplicationContext(),
                // login.class);
                // startActivity(i);

                finish();
            } else {
                // failed to create product
            }
        } 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 done
        // pDialog.dismiss();
    }
} }

しかし、私は目的の出力を取得しておらず、正確な問題を理解することさえできませんでした。私のLogcatは次のエラーを出します:

Error parsing data org.json.JSONException: Value Your of type java.lang.String cannot be converted to JSONObject

私が間違いを犯しているところを助けて、私を適切に導いてくださいどうすれば私のAndroidアプリケーションで希望の出力を得ることができますか?すぐに聞くことを望んでいます。前もって感謝します!

4

1 に答える 1

1

私の推測では、配列をJSONに出力していますが、解析しようとしていJSONObjectます。配列をエンコードする場合は、でデコードする必要がありますJSONArray。質問にJSONを表示すると、これが当てはまることが確認できます。

于 2012-11-27T00:48:46.767 に答える