SDK に含まれている http ライブラリを簡単に使用できます。例については、以下のコードを参照してください。これを使用して、ログイン、登録、カレンダーへのイベントの追加、およびイベントの取得を行います。
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
public class UserFunctions {
private JSONParser jsonParser;
// Testing in localhost using wamp or xampp
// use http://10.0.2.2/ to connect to your localhost ie http://localhost/
private static String loginURL = "http://xxx.com/android/login.php";
private static String registerURL = "http://xxx.com/android/login.php";
private static String createURL = "http://xxx.com/android/login.php";
private static String getEventURL = "http://xxx.com/android/login.php";
private static String login_tag = "login";
private static String create_tag = "create_event";
private static String register_tag = "register";
// constructor
public UserFunctions(){
jsonParser = new JSONParser();
}
/**
* function make Login Request
* @param email
* @param password
* */
public JSONObject loginUser(String email, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
if(json !=null)
Log.e("JSON", json.toString());
return json;
}
public JSONObject getEvents(String lon, String lat, String uid, String page){
// Building Parameters
Log.e("steve","public JSONObject getEvents(String lon, String lat, String uid)");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", "get_events"));
params.add(new BasicNameValuePair("lon", lon));
params.add(new BasicNameValuePair("lat", lat));
params.add(new BasicNameValuePair("uid", uid));
params.add(new BasicNameValuePair("page", page));
//Log.e("steve","Get Events");
JSONObject json = jsonParser.getJSONFromUrl(getEventURL, params);
// return json
if(json !=null)
Log.e("JSON", json.toString());
return json;
}
public JSONObject attendEvent(String uid, String eid){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", "attend_event"));
params.add(new BasicNameValuePair("uid", uid));
params.add(new BasicNameValuePair("eid", eid));
Log.e("steve","U="+uid+"E="+eid);
JSONObject json = jsonParser.getJSONFromUrl(getEventURL, params);
// return json
if(json !=null)
Log.e("JSON", json.toString());
return json;
}
public JSONObject getAttendance(String uid, String eid){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", "get_attendance"));
params.add(new BasicNameValuePair("uid", uid));
params.add(new BasicNameValuePair("eid", eid));
Log.e("steve","U="+uid+"E="+eid);
JSONObject json = jsonParser.getJSONFromUrl(getEventURL, params);
// return json
if(json !=null)
Log.e("JSON", json.toString());
return json;
}
/**
* function make Login Request
* @param name
* @param email
* @param password
* */
public JSONObject registerUser(String fname, String lname, String email, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("fname", fname));
params.add(new BasicNameValuePair("lname", lname));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
// return json
return json;
}
public JSONObject createEvent(String name, String edate, String etime, String edesc, String loc, String lon, String lat, String uid){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", create_tag));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("date", edate));
params.add(new BasicNameValuePair("time", etime));
params.add(new BasicNameValuePair("desc", edesc));
params.add(new BasicNameValuePair("loc", loc));
params.add(new BasicNameValuePair("lon", lon));
params.add(new BasicNameValuePair("lat", lat));
params.add(new BasicNameValuePair("uid", uid));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(createURL, params);
if(json !=null)
Log.e("JSON", json.toString());
// return json
return json;
}
/**
* Function get Login status
* */
public boolean isUserLoggedIn(Context context){
DatabaseHandler db = new DatabaseHandler(context);
int count = db.getRowCount();
if(count > 0){
// user logged in
return true;
}
return false;
}
/**
* Function to logout user
* Reset Database
* */
public boolean logoutUser(Context context){
DatabaseHandler db = new DatabaseHandler(context);
db.resetTables();
return true;
}
}