gpt4 book ai didi

php - Json 变量值丢失

转载 作者:行者123 更新时间:2023-11-30 01:09:03 24 4
gpt4 key购买 nike

我是android新手,我正在开发后端位于php mysql中的应用程序,现在我必须将成功变量值发送到android,但它错过了这些值,这里是我的php代码

<?php

include_once("conn.php");



$cdate= date("Y-m-d");

//$id = $_REQUEST['id'];
$uid = $_REQUEST['uid'];
$sql= "select * from user where user_id=$uid";
$result= mysql_query($sql);
while($row=mysql_fetch_assoc($result)){


$ldate = $row['created_date'];
}

$start = strtotime($ldate);

$end = strtotime($cdate);

$days_between = ceil(abs($end - $start) / 86400);
echo $days_between;
if($days_between<30){
$result= mysql_query($sql);
$json= array();


if(mysql_num_rows($result)){

while($row=mysql_fetch_assoc($result)){

//$json['emp_info'][]=$row;
}
$sval=1;
$mval=$rdays." days are remaining in expiration of your membership";
$json['emp_info']["suc"]=$sval;
$rdays= 30-$days_between;
$json['emp_info']["msg"]=$rdays." days are remaining in expiration of your membership";
}

echo json_encode($json);
}else{

//echo "you need to renew the membership";
$sval=0;
$mval="you need upgrade your membership to use the application";
$json= array();
$json['emp_info']["suc"]=$sval;
$json['emp_info']["msg"]=$mval;
echo json_encode($json);
}
?>

这是我的 android 代码来获取 json 的值

   // Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}

catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));

try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}

catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
Log.d("exception", "Exception is "+e.toString());
}
return answer;
}

@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task

public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}

// build hash set for list view
public void ListDrwaer() {
// List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();

try {
JSONObject jsonResponse = new JSONObject(jsonResult);
jsonMainNode = jsonResponse.optJSONArray("emp_info");
data = new String[jsonMainNode.length()];

for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String number = jsonChildNode.optString("user_id");
ID = number;
name = jsonChildNode.optString("suc");
pass = jsonChildNode.optString("msg");
Log.d("value", "values are"+name+pass);
Toast.makeText(getApplicationContext(), "values"+name+pass, Toast.LENGTH_LONG).show();
if(number.length()!=0){
Toast.makeText(getApplicationContext(), "Valeu of session before calling method "+session.IS_LOGIN, Toast.LENGTH_LONG).show();

session.IS_ID=ID;
Toast.makeText(getApplicationContext(), "value of session id "+session.IS_ID, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MyDashboard.this, MyDashboard.class);
intent.putExtra("ID", ID);



//SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd");
//String formattedDate = ate)Format.parse(date);
//datestr=formattedDate;
//date.split(date);
startActivity(intent);
//dologin();
//boolean value =true;
//session.createLoginSession(number, name,pass,true);
Toast.makeText(getApplicationContext(), "value of sesesion after calling method"+session.IS_LOGIN, Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Values"+name+number+pass, Toast.LENGTH_LONG).show();
//boolean value = false;
//session.createLoginSession(number, name, pass, value);
}
String outPut = name /*+ "-" + number*/;
// employeeList.add(createEmployee("employees", outPut));
//dologin();

/*session.editor.putBoolean(session.IS_LOGIN, true);

// Storing name in pref
session.editor.putString(session.KEY_PASS, pass);
// Storing id
session.editor.putString(session.IS_ID, ID);

// Storing email in pref
session.editor.putString(session.KEY_EMAIL, slemail);

Log.d("Session", "Values of session"+slemail +ID+pass);
// commit changes
Toast.makeText(getApplicationContext(), ""+session.IS_ID, Toast.LENGTH_LONG).show();
// session.editor.commit();*/
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}

}

现在在异步任务部分,它位于 catch 下而不是 try :S

最佳答案

emp_info 不是对象数组,因此您可能无法执行 JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);

可能您尝试打印的不是

{"emp_info":[1,"2 days are remaining in expiration of your membership"]}

但是

{"emp_info":[{"suc":1,"msg":"2 days are remaining in expiration of your membership"}]}

尝试这样的代码:

$rdays= 30-$days_between;
array_push($json['emp_info'], array(
"suc" => $sval;
"msg" => $rdays." days are remaining in expiration of your membership"
))

关于php - Json 变量值丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19583171/

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