简体   繁体   中英

Need Help Json Parsing Error Android

I've been trying to make an application which has very rich Database mySQL with PHP support on 000webhost. We're trying to calculate the mood of the person.

There are two tables Colour and Element from which i'm picking the Score on the basis of Colour_ID and Element_ID . Actually i took Colour_ID and Element_ID value from the user by android Application and passed it through JSON PARSING .

The Code of mood.php is:

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

Now what i want is that when user select 1 as Colour_ID and 1 as Element_ID , the mood on the Score 2 should be displayed to him on this android application.

The Mood.java is:

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

But i'm not getting the desired Output and couldn't even able to figure out the exact problem. My Logcat gives following Error :

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

Please help me where I'm making the mistake and Guide me properly How can i get my desired Output on my Android Application. Hoping to hear soon. Thanks in Advance!

My guess is that you are outputting an array to JSON, but trying to parse into JSONObject . If you are encoding an array, you need to decode with JSONArray . Show your JSON in the question and I can verify this is the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM