我遇到问题,Parse托管服务将于2017年1月28日退休;我需要迁移我的应用程序,我的sdk版本是1.8.2,当我移动到sdk版本1.13.1使用我自己的服务器但是我的Post类有问题,我有一个弃用的方法, refreshInBackground (com.parse.RefreshCallback)不推荐使用新方法就是这个,fetchInBackground(GetCallback)但是,我不知道如何使用它请帮助我,在此先感谢。
这是我的Post.java
package com.addict.secret;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.RefreshCallback;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Observable;
public class Post extends Observable {
private RecyclerView recyclerView;
itemViewAdapter itemViewAdapter;
private itemViewProfileAdapter itemViewProfileAdapter;
private Context context;
private ParseUser parseUser;
LocationManager locationManager;
LocationListener locationListener;
private int aroundMeNumber = 0;
private Date lastPositionDate;
private boolean needToLoad = false;
public Post(Context context) {
this.context = context;
}
public Post(Context context, RecyclerView recyclerView, itemViewAdapter itemViewAdapter) {
this.context = context;
this.recyclerView = recyclerView;
this.itemViewAdapter = itemViewAdapter;
}
public Post(Context context, RecyclerView recyclerView, itemViewAdapter itemViewAdapter, ParseUser parseUser) {
this.context = context;
this.recyclerView = recyclerView;
this.itemViewAdapter = itemViewAdapter;
}
public Post(Context context, RecyclerView recyclerView, itemViewProfileAdapter itemViewProfileAdapter, ParseUser parseUser) {
this.context = context;
this.recyclerView = recyclerView;
this.itemViewProfileAdapter = itemViewProfileAdapter;
this.parseUser = parseUser;
}
void grabPost(final boolean addingNewOnes) {
ParseGeoPoint userLocation = (ParseGeoPoint) ParseUser.getCurrentUser().get("Location");
ParseQuery<ParseUser> parseUserQuery = ParseUser.getQuery();
parseUserQuery.whereWithinKilometers("Location", userLocation, 1000.0);
Toast.makeText(context, R.string.loading, Toast.LENGTH_SHORT).show();
// Log.d("location", userLocation.toString());
parseUserQuery.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(final List<ParseUser> nearUsers, ParseException e) {
if (e == null) {
if (nearUsers.size() != 0) {
aroundMeNumber = nearUsers.size() - 1;
}
Toast.makeText(context, R.string.done, Toast.LENGTH_SHORT).show();
// Grabbing posts that are created by the people that are near the current user
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Post");
String[] nearUsersUsername = new String[nearUsers.size() + 1];
// Grabbing usernames from the near people result into array
for (int i = 0; i < nearUsers.size(); i++) {
nearUsersUsername[i] = nearUsers.get(i).getUsername();
}
nearUsersUsername[nearUsers.size()] = ParseUser.getCurrentUser().getUsername();
query.whereContainedIn("User", Arrays.asList(nearUsersUsername));
query.orderByDescending("createdAt");
query.setLimit(10);
if (addingNewOnes) {
query.whereLessThan("createdAt", lastPositionDate);
}
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> posts, ParseException e) {
if (posts.size() == 0) {
// Doing nothing
Toast.makeText(context, R.string.show_error, Toast.LENGTH_SHORT).show();
} else if (addingNewOnes && needToLoad) {
needToLoad = false;
itemViewAdapter.addItem(posts);
DefaultItemAnimator animator = new DefaultItemAnimator();
recyclerView.setItemAnimator(animator);
lastPositionDate = posts.get(posts.size() - 1).getCreatedAt(); // Save the last item's created-at date
} else {
itemViewAdapter = new itemViewAdapter(context, posts);
recyclerView.setAdapter(itemViewAdapter);
// itemViewAdapter.setClickListener(MainActivity.this); // Set on click listener on the post, may be used in the future
lastPositionDate = posts.get(posts.size() - 1).getCreatedAt(); // Save the last item's created-at date
setChanged();
notifyObservers();
}
}
});
} else {
final Dialog dialog = new Dialog(context, R.style.AppCompatAlertDialogStyle);
dialog.setContentView(R.layout.dialog_error);
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.message);
text.setText(R.string.error_update_message);
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.fox_network_error);
Button dialogButton = (Button) dialog.findViewById(R.id.ok);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
}
});
}
void searchPost(final boolean addingNewOnes) {
// Grabbing posts that are created by the people that are near the current user
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Post");
query.whereEqualTo("User", parseUser.getUsername());
query.orderByDescending("createdAt");
query.setLimit(10);
if (addingNewOnes) {
query.whereLessThan("createdAt", lastPositionDate);
}
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> posts, ParseException e) {
if (posts.size() == 0) {
// Doing nothing
Toast.makeText(context, R.string.show_error, Toast.LENGTH_SHORT).show();
} else if (addingNewOnes && needToLoad) {
needToLoad = false;
itemViewProfileAdapter.addItem(posts);
DefaultItemAnimator animator = new DefaultItemAnimator();
recyclerView.setItemAnimator(animator);
lastPositionDate = posts.get(posts.size() - 1).getCreatedAt(); // Save the last item's created-at date
} else {
itemViewProfileAdapter = new itemViewProfileAdapter(context, posts, ParseUser.getCurrentUser());
recyclerView.setAdapter(itemViewProfileAdapter);
// itemViewProfileAdapter.setClickListener(MainActivity.this); // Set on click listener on the post, may be used in the future
lastPositionDate = posts.get(posts.size() - 1).getCreatedAt(); // Save the last item's created-at date
setChanged();
notifyObservers();
}
}
});
}
void updateLocation() {
// boolean gpsEnabled = false;
// boolean networkEnabled = false;
// final float[] distanceResult = new float[1];
// Acquire a reference to the system Location Manager
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
final double latitude = location.getLatitude(); // x
final double longitude = location.getLongitude(); // y
final ParseGeoPoint currentLocation = new ParseGeoPoint(latitude, longitude);
ParseUser user = ParseUser.getCurrentUser();
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereEqualTo("objectId", user.getObjectId());
userQuery.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> parseUsers, ParseException e) {
if (e == null) {
ParseUser userFound = parseUsers.get(0);
// Checking the distance between current position and server user position
// ParseGeoPoint serverUserLocation = (ParseGeoPoint) userFound.get("Location");
// Location.distanceBetween(serverUserLocation.getLatitude(), serverUserLocation.getLongitude(), latitude, longitude, distanceResult);
userFound.put("Location", currentLocation);
userFound.saveInBackground();
// Toast.makeText(MainActivity.this, currentLocation.toString(), Toast.LENGTH_SHORT).show();
ParseUser.getCurrentUser().refreshInBackground(new RefreshCallback() {
@Override
public void done(ParseObject parseObject, ParseException e) {
// if (e == null){
// if (updateAnyway){
// post.grabPost(false);
// }
// else if (distanceResult[0] > 10000 && grabPostToo) {
// post.grabPost(false);
// }
// swipeRefreshLayout.setRefreshing(false);
// }
// else {
// Toast.makeText(MainActivity.this, "Error in updating user data", Toast.LENGTH_SHORT).show();
// }
if (e != null) {
Toast.makeText(context, R.string.error_update_user, Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(context, R.string.error_user, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
// try {
// gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// }
// catch (Exception e){
// Log.d("GPS Enabled", "False");
// }
//
// try {
// networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// }
// catch (Exception e){
// Log.d("Network Enabled", "False");
// }
//
// if (!gpsEnabled && !networkEnabled){
// Toast.makeText(MainActivity.this, "Location Provider is not available", Toast.LENGTH_SHORT).show();
// }
// if (gpsEnabled){
// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600000, 1000, locationListener);
// }
// else if (networkEnabled){
// locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 600000, 1000, locationListener);
// }
// Register the listener with the Location Manager to receive location updates
// Can also set interval between each check
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 600000, 1000, locationListener);
}
else {
Toast.makeText(context, R.string.location_off, Toast.LENGTH_SHORT).show();
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}
int getAroundMeNumber() {
return aroundMeNumber;
}
public void setAroundMeNumber(int aroundMeNumber) {
this.aroundMeNumber = aroundMeNumber;
}
public Date getLastPositionDate() {
return lastPositionDate;
}
public void setLastPositionDate(Date lastPositionDate) {
this.lastPositionDate = lastPositionDate;
}
boolean isNeedToLoad() {
return needToLoad;
}
void setNeedToLoad(boolean needToLoad) {
this.needToLoad = needToLoad;
}
}
答案 0 :(得分:0)
使用FetchInBackground时,还需要使用不同的回调类型。对于FetchInBackground,回调类型应该是 GetCallback (而不是RefreshCallback),因此您应该将代码更改为:
ParseUser.getCurrentUser().fetchInBackground(new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException e) {
if (e == null) {
// Success!
} else {
// Failure!
}
}
});