gpt4 book ai didi

android - 求助Android Json解析错误

转载 作者:行者123 更新时间:2023-11-30 03:54:26 25 4
gpt4 key购买 nike

我一直在尝试在 000webhost 上制作一个具有非常丰富的数据库 mySQL 和 PHP 支持的应用程序。我们正在尝试计算此人的情绪。

有两个表ColourElement,我根据Colour_ID 从中选择Score> 和 Element_ID。实际上,我通过 Android 应用程序从 user 获取了 Colour_IDElement_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_ID1 作为 Element_ID 时,Score 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 应用程序上获得我想要的输出。希望早日听到。提前致谢!

最佳答案

我的猜测是您正在将数组输出为 JSON,但试图解析为 JSONObject。如果您正在对数组进行编码,则需要使用 JSONArray 进行解码。在问题中显示您的 JSON,我可以验证情况是否如此。

关于android - 求助Android Json解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13575684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com