我已经在运行使用Parse.com后端服务的Android应用。我需要应用程序来调用解析服务器并使用mLab数据库。您能否向我展示如何在我的Android应用程序中发送数据并从mLab(在AWS上使用Parse Server)获取数据的代码示例。 Parse服务器已启动并正在运行。我已经将我的数据库从Parse.com迁移到mLab。 mLab和Parse服务器已连接。
这是我在调用parse.com网址时使用的代码 -
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);我该如何更改此代码以指向Parse服务器
答案 0 :(得分:0)
那么,
要使用parse.com网站,您的代码中可能会有这样的地方:
Parse.initialize(aContext, applicationId, clientKey);
您现在需要将其替换为
Parse.initialize(new Parse.Configuration.Builder(aContext)
.applicationId(applicationId)
.server(urlOfYourOwnParseServer)
.build());
就是这样......你已经完成了。