AsyncHttpClient not working - java

I have the following timer in onCreate():
new Timer().scheduleAtFixedRate(new TimerTask(){
public void run() {
Log.i("EOH","timer");
updateMarkers();
}}, 0, 1000);
As you can see, it calls the function updateMarkers() every second.
Here is updateMarkers():
private void updateMarkers(){
Log.i("EOH","updateMarkers()");
Drawable drawable = this.getResources().getDrawable(R.drawable.google_maps_marker);
final MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable,mapView);
AsyncHttpClient myClient = new AsyncHttpClient();
final PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
myClient.setCookieStore(myCookieStore);
RequestParams params = new RequestParams();
params.put("sw_lat", String.valueOf(centrePoint.getLat()-(Double.valueOf(mapView.getLatitudeSpan())/2.0)));
params.put("sw_lng", String.valueOf(centrePoint.getLat()-(Double.valueOf(mapView.getLongitudeSpan())/2.0)));
params.put("ne_lat", String.valueOf(centrePoint.getLat()+(Double.valueOf(mapView.getLatitudeSpan())/2.0)));
params.put("ne_lng", String.valueOf(centrePoint.getLat()+(Double.valueOf(mapView.getLongitudeSpan())/2.0)));
myClient.post("http://www.prestocab.com/ajax/getRanks.php", params, new AsyncHttpResponseHandler() {
public void onStart() {
thinger.setVisibility(View.VISIBLE);
Log.i("EOH","onStart()");
}
public void onSuccess(String response) {
Log.i("EOH","xxx: "+response);
thinger.setVisibility(View.INVISIBLE);
try{
JSONArray arr=(JSONArray) new JSONTokener(response).nextValue();
Log.i("EOH","yyy: "+String.valueOf(arr.length()));
}catch(JSONException e){
}
}
public void onFailure(Throwable e) {
thinger.setVisibility(View.INVISIBLE);
}
public void onFinish() {
}
});
}
The problem I'm having is that Log.i("EOH","onStart()"); never gets called! However Log.i("EOH","updateMarkers()"); gets called...
What am I doing wrong?
Many thanks in advance,

I found out that current implementation of AsyncHttpClient - the first I start using - version 1.4.3 (2013-09-25) has some problem with local intranet hostnames. I was testing using a local network with the webserver running on my computer and it was calling onStart, but never calling onSuccess or any other methods.
My hostname was RM_UB02 (without the Internet domain suffixes). Android and Chrome web browsers were able to resolve the URL (http:// RM_UB02/test/page.html), but AsyncHttpClient never worked.
I changed to the local intranet IP address (192.168.1.104) and it started working.
I figured out that it was working all right for Internet names (http://slashdot.org ), but not for local networks.

#Override
public void onStart()
{
}
#Override
public void onFinish()
{
}
#Override
public void onSuccess()
{
}
#Override
public void onFailure()
{
}

Related

How to notify UI on volley success MVVM architecture

I am using mvvm architecture I would like to notify view when volley post request is successful, what i could do is to instantiate ViewModel in appRepository class and then post values to a liveData, but i guess that's not a good approach as I haven't seen a similar practice. Can anyone suggest me a good approach to return my response to ui, or at least notify that post request has been successful.
From fragment/View I trigger this method
// save data to api
checkInViewModel.updateEventPersonEntity(eventPersonsEntity);
ViewModel forwards it to apprespository
public void updateEventPersonEntity(EventPersonsEntity eventPersonsEntity) {
mRepository.updateEventPersonEntity(eventPersonsEntity);
}
AppRepository.Java class
public void updateEventPersonEntity(EventPersonsEntity eventPersonsEntity) {
executor.execute(() -> {
// mDb.eventPersonsDao().update(eventPersonsEntity);
if (isNetworkAvailable(context)) {
post_updateEventPersonEntity(eventPersonsEntity);
}
});
}
private void post_updateEventPersonEntity(EventPersonsEntity eventPersonsEntity) {
Map<String, Object> params = new HashMap<>();
params.put("EventPersonId", eventPersonsEntity.getEventPersonId());
params.put("EventId", eventPersonsEntity.getEventId());
params.put("PersonId", eventPersonsEntity.getPersonId());
params.put("CashStart", parseDoubleToGerman(eventPersonsEntity.getCashStart()));
params.put("CashEnd", parseDoubleToGerman(eventPersonsEntity.getCashEnd()));
params.put("StartingTime", String.valueOf(eventPersonsEntity.getStartingTime()));
params.put("EndingTime", String.valueOf(eventPersonsEntity.getEndingTime()));
params.put("isChekcedIn", eventPersonsEntity.getIsCheckedIn());
params.put("isChekcedOut", eventPersonsEntity.getIsCheckedOut());
JSONObject objRegData = new JSONObject(params);
String eventPersonApi = APP_URL.EVENT_PERSONS_API + eventPersonsEntity.getEventPersonId();
RequestQueueSingleton.getInstance(context).objectRequest(eventPersonApi, Request.Method.PUT, this::onSuccess_updateEventPersonEntity, this::onError, objRegData);
}
private void onError(VolleyError error) {
Log.d(APP_REPOSITORY_TAG, "requestError: " + error);
}
private void onSuccess_updateEventPersonEntity(JSONObject jsonObject) {
// notify ui
}
You can do this same as you did for your success response logic in repository. Simply create new callback interface:
interface OnEventUpdatedListener{
void eventUpdated();
}
Then, update your method to look like this, passing the listener to the actual method that does the work:
public void updateEventPersonEntity(EventPersonsEntity eventPersonsEntity, OnEventUpdatedListener listener) {
mRepository.updateEventPersonEntity(eventPersonsEntity, listener);
}
Pass this inside your:
if (isNetworkAvailable(context)) {
post_updateEventPersonEntity(eventPersonsEntity, listener);
}
After that, in your onSuccess() method simply call:
private void onSuccess_updateEventPersonEntity(JSONObject jsonObject) {
listener.eventUpdated();
}
Finally, you will have the info when the update happens, in the calling site, if you call your repository like this:
updateEventPersonEntity(null, new OnEventUpdatedListener() {
#Override
public void EventUpdated() {
// Do your logic here
}
});

Android - Java Equivalent Of iOS Block Callbacks?

In my Android app, I'd like to implement success and error callbacks for when I get reading passages from my backend. In iOS, it would look like this:
In my Passage.h:
-(void)getPassagesWithSuccessCallback:(void (^)(NSArray<Passage *> *))success errorCallback:(void (^)(NSString *))errorString;
In my Passage.m:
-(void)getPassagesWithSuccessCallback:(void (^)(NSArray<Passage *> *))success errorCallback:(void (^)(NSString *))errorString {
MyApiInterface* api = [MyApiInterface sharedInstance];
[api sendGetRequestTo:#"passages" successCallback:[Passage modelListCallback:success] errorCallback:error];
}
In my Android app, I'm using Volley to handle my API requests, but I want to further encapsulate this API interfacing by having a Passage.java class with a public static void method that gets the passages. Something like this:
public static void getPassagesForFirebaseUser(FirebaseUser user, Context context) {
final String url = URL_BASE + "/passages.json" + "?auth=" + user.getToken(false);
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// convert JSON into ArrayList<Passage> object
// pass on this array of Passages in the success completion listener of the method that called this
// just like iOS does success(passages)
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// convert error to string
// pass on this errorString in the error completion listener of the method that called this
// just like iOS does error(errorString)
}
});
Volley.newRequestQueue(context).add(request);
}
Is there any way to get this kind of implementation flow?
You can use an Interface
public interface ICallbacks {
public void onResponse(JSONObject response);
public void onError(VolleyError error);
}
Then in your routine code just put a new instance of Callbacks (depending on ide that you work could autogenerate the methods)
public static void getPassagesForFirebaseUser(FirebaseUser user,
Context context, ICallbacks events) {
//here code and call ICallbacks methods
if(result){ events.onResponse(response); }
if(error){ events.onError(err); }
}
ultimately you can call the method with :
getPassagesForFirebaseUser(user, context, new ICallbacks(){
#Override
public void onResponse(JSONObject response){
//Success !!!
}
#Override
public void onError(VolleyError response){
//Error !!!
}
});
Sorry for my English, hope this help !

Android Volley: Static vs Object

I am a junior android developer and I almost finished the alpha version of my first big project. I think that I have good knowledge of java but I am not sure if I organized my app right.
Short description: I use in my app the volley library to send and receive data from server. Because of that I created a class to manage server methods. In that class I created a lot of static methods for every connection to server I need(like this example):
public static void sendDataToServer(final Context context, final String data) {
StringRequest mStringRequest = new StringRequest(Request.Method.POST, URL_VERIFY, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// get response
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// get error response
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
// the POST parameters:
params.put(API_KEY, API_KEY_VALUE);
params.put(API_KEY_DATA, data);
return params;
}
};
Volley.newRequestQueue(context).add(mStringRequest);
}
So in my activities I call this like MyServerClass.sendDataToServer(...)
My question is: Is it ok to call my server methods like that? Or should I make them instance methods and instantiate MyServerClass when activity is started? I must mention that I have about 5 methods in that class.
I have another class like that with methods to check data accuracy. Should I also make them instance methods and instantiate it in the activities I need?
Any reference or advice is welcome. Thanks in advance!
No, in your case, both ways will have the same result...
The only thing to mention is, that if you need to receive the response to your request too (may be in the future), you will need to add a Delegate / Callback / Interface to your class, to get the result right back to your calling activity instance... In that case it would be better to create a "non-static instance method" way... But you can add a non-static Method to your Class too so I don't see anything against it.
UPDATE TO COMMENT
Well for example, if you want to provide a ListView with Images... In most cases you first request an JSONArray with your ListView entries, which contains the links to Bitmaps located on the remote Server...
If you download Images Async and put them into the ImageViews in the rows of a ListView (while the user scrolls), it could be possible that images are loaded longer and the ListView will show images in wrong places... For something like that you will need a Singleton Pattern, which will manage the downloads for you... This will not be possible with your class/static Method
Although this question has already had an accepted answer, however, I'd like to share my code that looks like your issue. Hope this helps!
I also use Interface like #Neo answer, as the following:
public interface VolleyResponseListener {
void onError(String message);
void onResponse(Object response);
}
Then in my VolleyUtils class:
public static void makeJsonObjectRequest(Context context, String url, final VolleyResponseListener listener) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
listener.onResponse(response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
listener.onError(error.toString());
}
}) {
#Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
};
// Access the RequestQueue through singleton class.
VolleySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest);
}
Then in Activity:
VolleyUtils.makeJsonObjectRequest(mContext, url, new VolleyResponseListener() {
#Override
public void onError(String message) {
}
#Override
public void onResponse(Object response) {
}
});
P/S: my project uses Google's official Volley library, instead of using compile 'com.mcxiaoke.volley:library:1.0.17' in build.gradle. As a result, JsonObjectRequest(...) will have a difference at its definition.

Use Facebook SDK For Invitation Android

I am trying to use the Android SDK to "invite friends', however my code is giving the following error...
"failed to find provider info for
com.facebook.katana.provider.attributionid"
Could you please review my code below and help me understand what I am doing wrong.
String appLinkUrl, previewImageUrl;
appLinkUrl = "your app link url";
previewImageUrl = "https://www.example.com/my_invite_image.jpg";
if (AppInviteDialog.canShow())
{
AppInviteContent content = new AppInviteContent.Builder()
.setApplinkUrl(appLinkUrl)
.setPreviewImageUrl(previewImageUrl)
.build();
AppInviteDialog appInviteDialog = new AppInviteDialog(MainActivity.this);
appInviteDialog.registerCallback(callbackManager, new FacebookCallback<AppInviteDialog.Result>()
{
#Override
public void onSuccess(AppInviteDialog.Result result)
{
}
#Override
public void onCancel()
{
}
#Override
public void onError(FacebookException e)
{
}
});
appInviteDialog.show(content);
}

Google Cloud Endpoints Setup Trouble

I'm completely new to Google's Cloud Platform and I'm having trouble setting it up for my Android device. I am attempting to follow this tutorial and I'm at the point of trying to test my backend with my Android Emulator. The emulator, however, gives me this message after 20 seconds, Where instead it should say my name. Here's my code so far:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buildUI();
new EndpointsAsyncTask().execute(new Pair<Context, String>(this, "Solomon"));
}
EndpointsAsyncTask.java
public class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
private static MyApi myApiService = null;
private Context context;
#Override
protected String doInBackground(Pair<Context, String>... params) {
if(myApiService == null) { // Only do this once
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
#Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
// end options for devappserver
myApiService = builder.build();
}
context = params[0].first;
String name = params[0].second;
try {
return myApiService.sayHi(name).execute().getData();
} catch (IOException e) {
return e.getMessage();
}
}
#Override
protected void onPostExecute(String result) {
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}
All Help is appreciated!
EDIT: Part of the problem was that I was running Endpoints Backend rather than the App Engine Servlet Backend. But now I'm now getting "connection refused" and I am running the App Engine Servlet Backend. Any Ideas?
After a tough few days, i found that the problem was that I needed to change my rootUrl from http://10.0.2.2:8080/_ah/api/ to my appspot domain. Now I'm getting the Hello World message.

Categories

Resources