0

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

这是我获取json值的android代码

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

 }

现在在 asyncx 任务部分它位于 catch 而不是 try :S

4

2 回答 2

0

错误在你的 php 代码中,我对其进行了一些修改,看看它是否有效

<?php

require_once './db_connect.php';  //mine
$db = new DB_CONNECT();           //mine

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

//$id = $_REQUEST['id'];
$uid = 1;//$_REQUEST['uid'];       //change to your and check if $_REQUEST IS SET
                                   //use if(isset($_POST["v_id"])) 

$sql= "select * from user where id=$uid";
$result= mysql_query($sql);
while($row=mysql_fetch_array($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_array($result)){

//$json['emp_info'][]=$row;
}
$sval=1;

$rdays= 30-$days_between;  //this was below in original

$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); 
}
?>
于 2013-10-25T07:27:59.137 回答
0

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"
))
于 2013-10-25T08:29:00.267 回答