私はアンドロイドで初めてで、バックエンドがphp mysqlにあるアプリケーションに取り組んでいます。今、成功変数の値をアンドロイドに送信する必要がありますが、ここの値が欠けていました私の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();
}
}
aynx タスク セクションでは、try :S の代わりに catch の下に移動します。