我一直在尝试在 000webhost 上制作一个具有非常丰富的数据库 mySQL 和 PHP 支持的应用程序。我们试图计算这个人的情绪。
有两个表Color和Element我根据Colour_ID和Element_ID从中挑选分数。实际上,我通过 android Application 从用户那里获取了Colour_ID和Element_ID值,并通过JSON PARSING传递了它。
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);
}
?>
现在我想要的是当用户选择 1作为Colour_ID和1 作为 Element_ID时,分数 2上的情绪应该在这个 android 应用程序上显示给他。
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 应用程序上获得我想要的输出。希望尽快听到。提前致谢!