我一直在尝试在000webhost上创建一个具有非常丰富的Database mySQL和PHP支持的应用程序。我们正试图计算这个人的心情。
有两个表颜色和元素,我根据 Colour_ID 得分 >和 Element_ID 。实际上我通过Android应用程序从用户获取了 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应用程序上获得所需的输出。希望尽快听到。在此先感谢!
答案 0 :(得分:1)
我的猜测是你输出一个数组到JSON,但试图解析成JSONObject
。如果要对数组进行编码,则需要使用JSONArray
进行解码。在问题中显示您的JSON,我可以验证是这种情况。