我正在尝试开发一个 Android 应用程序,但我觉得有点卡在实现细节上。
我希望应用程序与 Web 服务器 (LAMP) 连接。然后,我的 PHP 页面将等待来自我的 Android 设备的信号,并在触发事件时重定向页面。
经过一番研究,我已经扣除了插座之一,尽管我不太确定。Android 设备连接到服务器上的套接字,并在事件完成时发送重定向信号。有没有其他方法可以做到这一点?
此外,我对服务器语言的其他用途是处理 POST 请求并将它们提交到数据库。有没有比 PHP 更适合的语言?
您可以简单地使用 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;
}
}