2013-06-11 72 views
1

我想開發一個Android應用程序,我覺得有點卡在實現細節。PHP等待信號和從Android應用程序重定向

我想讓應用程序與網絡服務器(LAMP)連接。然後,我的PHP頁面會等待來自Android設備的信號,並在事件被觸發時重定向頁面。

經過一番研究,我已經扣除了這個套接字,雖然我不太確定。 Android設備連接到服務器上的套接字,並在事件完成時發送重定向信號。有沒有其他的方法來完成這個?

此外,我對服務器語言的其他用法是處理POST請求並將它們提交給數據庫。與PHP相比,是否有更適合的語言?

+0

我不太確定套接字的東西。需要在android上使用套接字的情況非常少見。在你的情況下,爲什麼不在電話和燈服務器之間做傳統的http請求?而在服務器端,做一些Ajax輪詢來更新頁面? – Gomoku7

回答

0

您可以簡單地使用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; 
} 

}