Android Open Source - ChattyOwl Properties Loader






From Project

Back to project page ChattyOwl.

License

The source code is released under:

MIT License

If you think the Android project ChattyOwl listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.znupy.chattyowl.network;
/*www . j  ava  2  s  .com*/
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.znupy.chattyowl.R;
import com.znupy.chattyowl.activities.MainActivity;
import com.znupy.chattyowl.models.Property;

import java.util.List;

/**
 * Created by samok on 28/07/14.
 */
public class PropertiesLoader {
    private static final String TAG = PropertiesLoader.class.getSimpleName();

    private static final int SOCKET_TIMEOUT_MS = 30000;
    private static final int MAX_RETRIES = 0;

    private final Context context;
    private final RequestQueue requestQueue;

    private String serverUrl;

    public PropertiesLoader(Context context) {
        this.context = context;
        requestQueue = ((MainActivity)context).getRequestQueue();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        serverUrl = prefs.getString("properties_endpoint", context.getString(R.string.pref_default_endpoint));
    }

    public void get(final ResponseListener listener) {

        GsonRequest<PropertiesResponse> request = new GsonRequest<PropertiesResponse>(
                Request.Method.GET,
                serverUrl,
                null,
                PropertiesResponse.class,
                new Response.Listener<PropertiesResponse>() {

                    @Override
                    public void onResponse(PropertiesResponse response) {
                        Log.d(TAG, "onResponse: " + response);

                        if(listener == null) return;
                        if(response.success)
                            listener.onSuccess(response.properties);
                        else
                            listener.onError(response.message == null ?
                                    context.getString(R.string.response_unknown_error) :
                                    response.message);
                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d(TAG, "onErrorResponse: " + error);
                        if(listener == null) return;

                        listener.onError(error.getMessage());
                    }
                }
        );

        request.setRetryPolicy(new DefaultRetryPolicy(
                SOCKET_TIMEOUT_MS,
                MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


        requestQueue.add(request);
    }

    public interface ResponseListener {
        public void onSuccess(List<Property> properties);
        public void onError(String message);
    }
}




Java Source Code List

com.znupy.chattyowl.ApplicationTest.java
com.znupy.chattyowl.activities.MainActivity.java
com.znupy.chattyowl.activities.SettingsActivity.java
com.znupy.chattyowl.models.Property.java
com.znupy.chattyowl.network.BaseResponse.java
com.znupy.chattyowl.network.CommandCenter.java
com.znupy.chattyowl.network.GsonRequest.java
com.znupy.chattyowl.network.PropertiesLoader.java
com.znupy.chattyowl.network.PropertiesResponse.java
com.znupy.chattyowl.ui.SoundIndicator.java
com.znupy.chattyowl.widgets.PropertiesWidget.java
com.znupy.chattyowl.widgets.VoiceWidget.java