我有一個已在運行的Android應用程序使用Parse.com後端服務。我需要應用程序調用解析服務器,並使用mLab數據庫。您可以向我展示代碼示例,瞭解如何在我的Android應用程序中發送數據並從mLab獲取數據(使用AWS上的Parse Server)。 Parse服務器已啓動並正在運行。我已經將我的數據庫從Parse.com遷移到mLab。 mLab和Parse服務器已連接。遷移到解析服務器(在AWS上)和mLab for Android應用
這是我使用的代碼時,我稱之爲parse.com URL-
package com.gendertimerpro.services;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import com.gendertimerpro.logger.Logger;
public class Utils {
public static String URL_SERVER_ADDRESS = "http://108.xxx.162.xxx/MyApp/";
public static String REGISTER = "user_register.php";
public static String LOGIN = "login.php";
public static String FORGOT_PASSWORD = "forgot_password.php";
public static String CREATE_GROUP = "create_group.php";
public static String SAVE_SESSION = "save_session.php";
public static String GET_MY_GROUP = "get_my_group.php";
public static String GET_ALL_SESSION = "get_all_session.php";
public static String SEND_FEEDBACK = "send_feedback.php";
public static String PREF_NAME = "gendertimerpref";
public static String TAG = "Utils";
public static String USER_EMAIL = "email";
public static String USER_PASSWORD = "password";
public static String USER_ID = "user_id";
public static String GENDER_TYPE = "gender_type";
public static String Two_Three = "two_three";
// twitter
public static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
public static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret";
public static final String PREF_KEY_TWITTER_LOGIN = "is_twitter_loggedin";
public static final String TWITTER_USER_NAME = "twitter_user_name";
public static final String TWITTER_ID = "twitter_id";
// USER
public static final String PARSE_FACEBOOK_ID = "authData";
public static final String PARSE_USER_ID = "user_id";
public static final String PARSE_OBJECT_ID = "objectId";
// GROUP
public static final String PARSE_GROUP_NAME = "name";
// public static final String PARSE_GROUP_NAME_CREATE_GROUP = "group_name";
public static final String PARSE_NO_SESSIONS = "sessionCount";
//public static final String PARSE_GROUP_RATIO = "points";
// public static final String PARSE_GROUP_RATIO = "group_ratio";
// public static final String PARSE_GROUP_SCORE = "points";
// public static final String PARSE_LAST_SESSION = "last_session";
// public static final String PARSE_LAST_SESSION_JOINTEXT = "lastSession";
// SESSION
public static final String PARSE_SESSION_NAME = "name";
public static final String PARSE_NOTES = "notes";
public static final String PARSE_NO_OF_MALE_SPEAKER = "menCount";
public static final String PARSE_MALE_SPEAKER_TIMER = "menTime";
public static final String PARSE_MALE_SLOTS = "menSpeakCount";
public static final String PARSE_NO_OF_FEMALE_SPEAKER = "womenCount";
public static final String PARSE_FEMALE_SPEAKER_TIMER = "womenTime";
public static final String PARSE_FEMALE_SLOTS = "womenSpeakCount";
public static final String PARSE_NO_OF_THIRD_GENDER_SPEAKER = "thirdGenderCount";
public static final String PARSE_THIRD_GENDER_SPEAKER_TIMER = "thirdGenderTime";
public static final String PARSE_THIRD_GENDER_SLOTS = "thirdGenderSpeakCount";
// public static final String PARSE_SCORE = "score";
public static final String PARSE_PRIVACY = "public";
//public static final String PARSE_RATIO = "points";
// public static final String PARSE_RATIO = "group_ratio";
public static final String PARSE_POINT = "points";
public static final String PARSE_GROUP_ID = "group";
public static final String PARSE_GEO_POINT = "coord";
// public static final String PARSE_LATITUDE = "latitude";
// public static final String PARSE_LONGITUDE = "longitude";
//public static final String TWO_THREE = "two_three";
// FEEDBACK
public static final String PARSE_FEEDBACK_EMAIL = "email";
public static final String PARSE_FEEDBACK_MESSAGE = "message";
public static final String PARSE_FEEDBACK_USER_ID = "user";
public static boolean validateString(String object) {
boolean flag = false;
if (object != null && !object.isEmpty()
&& object.equalsIgnoreCase("null") != true
&& object.trim().length() > 0
&& !object.equalsIgnoreCase("(null)")) {
flag = true;
}
return flag;
}
public static String postRequest(String url,
List<NameValuePair> nameValuePairs) {
String request = "";
String result = null;
try {
Logger.e(TAG, "url:: " + url);
for (NameValuePair nvp : nameValuePairs) {
request += nvp.getName() + "=" + nvp.getValue() + "&";
}
Logger.e(TAG, "request:: " + request);
// Execute HTTP Post Request
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is
// established.
int timeoutConnection = 200000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT) in milliseconds which
// is the timeout for waiting for data.
int timeoutSocket = 200000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response1 = httpclient.execute(httppost);
result = EntityUtils.toString(response1.getEntity());
Logger.i("TAG", "result -- " + result);
int maxLogSize = 1000;
int start = 0, end = 0;
for (int i = 0; i <= result.length()/maxLogSize; i++) {
start = i * maxLogSize;
end = (i + 1) * maxLogSize;
end = end > result.length() ? result.length() : end;
Logger.i("TAG", "result str -- " + result.substring(start, end));
}
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String postRequest(String url) {
String result = null;
try {
Logger.e("TAG", "url:: " + url);
/*
* for (NameValuePair nvp : nameValuePairs) { String name =
* nvp.getName(); String value = nvp.getValue(); Loggger.e("TAG",
* name +"="+value); }
*/
// Execute HTTP Post Request
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is
// established.
int timeoutConnection = 200000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT) in milliseconds which
// is the timeout for waiting for data.
int timeoutSocket = 200000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response1 = httpclient.execute(httppost);
result = EntityUtils.toString(response1.getEntity());
int maxLogSize = 1000;
int start = 0, end = 0;
for (int i = 0; i <= result.length()/maxLogSize; i++) {
start = i * maxLogSize;
end = (i + 1) * maxLogSize;
end = end > result.length() ? result.length() : end;
Logger.i("TAG", "" + result.substring(start, end));
}
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static GPSTracker getCurrentLocation(Context context) {
GPSTracker gps = new GPSTracker(context);
// check if GPS enabled
if (gps.canGetLocation()) {
// double latitude = gps.getLatitude();
// double longitude = gps.getLongitude();
return gps;
} else {
// can't get location // GPS or Network is not enabled // Ask user
// to enable GPS/network in settings
gps.showSettingsAlert();
}
return null;
}
//method for checking if the password matches
public static boolean checkIfPasswordMatches(String firstPass, String secondPass){
boolean flag=true;
if(!firstPass.equals(secondPass)){
flag = false;
}
return flag;
}
public static boolean checkInternetConnection(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
} else {
// Toast.makeText(context, "Conex�o com a internet indispon�vel.",
// Toast.LENGTH_SHORT).show();
return false;
}
}
public static final void showMessageDialog(Context context, String title,
String message) {
if (message != null && message.trim().length() > 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setCancelable(false);
builder.setMessage(message);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
// create alert dialog
AlertDialog alertDialog = builder.create();
// show it
alertDialog.show();
}
}
public static boolean isEmailValid(String email) {
String expression = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
CharSequence inputStr = email;
boolean flag = false;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
flag = true;
}
return flag;
}
}
正如你看到的,我不使用Parse.initialize(aContext,的applicationID,clientKey);我應該如何改變這種代碼,以解析服務器
您是否設置瞭解析服務器? –
是的,我只是不理解解析提供的代碼示例。我需要一個非常容易的exempel –
猜猜看,你沒有使用解析:)也許,有時候你做了,但知道你的電話是針對http://108.xxx.162.xxx/MyApp/,所以你有一個解決您自己的Web服務。 –