Android Open Source - Netatmo-API-Android Netatmo Response Handler






From Project

Back to project page Netatmo-API-Android.

License

The source code is released under:

Apache License

If you think the Android project Netatmo-API-Android 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

/*
 * Copyright 2013 Netatmo//  ww w  .  ja  v  a 2  s  .c o m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netatmo.weatherstation.api;

import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import android.util.Log;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.netatmo.weatherstation.api.model.Measures;
import com.netatmo.weatherstation.api.model.Station;

/**
 * Netatmo's implemantation of {@link com.loopj.android.http.JsonHttpResponseHandler}.
 * You can use this handler that parses and returns the different responses from the Netatmo API calls.
 * See the sample package for examples.
 */
public class NetatmoResponseHandler extends JsonHttpResponseHandler {
    public static final int REQUEST_LOGIN = 0;
    public static final int REQUEST_GET_DEVICES_LIST = 1;
    public static final int REQUEST_GET_LAST_MEASURES = 2;

    static final String TAG = "NetatmoResponseHandler";

    NetatmoHttpClient mHttpClient;
    int mRequestType;
    String[] mMeasuresTypes;

    /**
     * @param requestType Used by {@link #onSuccess(org.json.JSONObject)} to return the right response.
     * You must use the constant corresponding to your request:
     * {@link #REQUEST_LOGIN}, {@link #REQUEST_GET_DEVICES_LIST}, {@link #REQUEST_GET_LAST_MEASURES}.
     * @param httpClient Your instance of {@link NetatmoHttpClient}.
     * @param measuresTypes Required for {@link #REQUEST_GET_LAST_MEASURES} only.
     */
    public NetatmoResponseHandler(NetatmoHttpClient httpClient, int requestType, String[] measuresTypes) {
        super();
        mHttpClient = httpClient;
        mRequestType = requestType;
        mMeasuresTypes = measuresTypes;
    }

    public void onLoginResponse() {}
    public void onGetDevicesListResponse(List<Station> devices) {}
    public void onGetMeasuresResponse(HashMap<String, Measures> measures) {}

    @Override
    public void onSuccess(JSONObject response) {
      
      final String M = "onSuccess: ";
        //Log.i(TAG, M);
      
        super.onSuccess(response);

        switch (mRequestType) {
            case REQUEST_LOGIN:
                mHttpClient.processOAuthResponse(response);
                onLoginResponse();
                break;
            case REQUEST_GET_DEVICES_LIST:
                List<Station> list = NetatmoUtils.parseDevicesList(response);
                onGetDevicesListResponse(list);
                break;
            case REQUEST_GET_LAST_MEASURES:
                HashMap<String, Measures> measures = NetatmoUtils.parseMeasures(response, mMeasuresTypes);
                onGetMeasuresResponse(measures);
                break;
            default:
                break;
        }
    }

    @Override
    public void onFailure(Throwable e, JSONObject errorResponse) {
      final String M = "onFailure: ";
        Log.i(TAG, M + errorResponse);
      
      
        super.onFailure(e, errorResponse);
    }

    @Override
    public void onFailure(Throwable error, String content) {
      
      final String M = "onFailure: ";
      Log.i(TAG, M + content);
      
        super.onFailure(error, content);

    }
}




Java Source Code List

com.netatmo.weatherstation.api.HttpUrlConnectionService.java
com.netatmo.weatherstation.api.NetatmoErrorCodes.java
com.netatmo.weatherstation.api.NetatmoHttpClient.java
com.netatmo.weatherstation.api.NetatmoResponseHandler.java
com.netatmo.weatherstation.api.NetatmoUtils.java
com.netatmo.weatherstation.api.model.Measures.java
com.netatmo.weatherstation.api.model.Module.java
com.netatmo.weatherstation.api.model.Params.java
com.netatmo.weatherstation.api.model.Station.java
com.netatmo.weatherstation.sample.CustomAdapter.java
com.netatmo.weatherstation.sample.LoginActivity.java
com.netatmo.weatherstation.sample.MainActivity.java
com.netatmo.weatherstation.sample.SampleHttpClient.java