2013-10-25 62 views
0

我是新的Android和我正在研究應用程序的後端是在PHP的MySQL,現在我必須發送成功的變量值的Android,但它錯過了這裏的值是我的PHP代碼Json變量值丟失

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

} 

在它去捕捉,而不是嘗試下asynx任務是:S

+0

顯示您的logcat輸出或錯誤日誌 – SilentKiller

+0

@SilentKiller 3月10日至25日:08:52.409:d /錯誤(3298):org.json.JSONException:值

+0

Waleed請提供完整的錯誤塊。 – SilentKiller

回答

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" 
)) 
+0

這給null –

+0

@WaleedAhmed在哪裏? – Tishka17

+0

{「emp_info」:null}你的代碼返回這個 –

0

釷E錯誤是在你的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); 
} 
?> 
+0

不,這是不是解決方案我的PHP工作正常http://aamirarif.com/organizer_json/mobile/test/testpayment.php?uid=72 –

+0

刪除echo $ days_between;從你的php代碼 – vishal091166

+0

問題是保持不變 –