org.maikelwever.droidpile.backend.TestApiConnecter.java Source code

Java tutorial

Introduction

Here is the source code for org.maikelwever.droidpile.backend.TestApiConnecter.java

Source

/*
 * This file is part of Droidpile: a Mailpile client for Android.
 *
 * You should have recieved a copy of the LICENSE file along with Droidpile.
 * If not: see <https://github.com/maikelwever/droidpile/blob/master/LICENSE>
 */
package org.maikelwever.droidpile.backend;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.maikelwever.droidpile.R;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;

/**
 */
public class TestApiConnecter extends ApiConnecter {

    private HashMap<String, String> testGetData;
    private HashMap<String, String> testPostData;
    private ApiConnecter properApi;

    public TestApiConnecter(Context context) {
        super(context);
        properApi = new ApiConnecter(context);
        initialize(context);
    }

    /*
    public TestApiConnecter(String baseUrl) {
    super(baseUrl);
    properApi = new ApiConnecter(baseUrl);
    initialize();
    }
    */

    @Override
    public String getData(boolean cache, String... data) throws IOException, JSONException {
        String suffix = "";
        for (String arg : data) {
            suffix += "/" + arg;
        }

        if (testGetData.containsKey(suffix)) {
            return testGetData.get(suffix);
        } else {
            Log.d("droidpile", "Error: data not in testdata. Url: " + suffix);
            return null;
        }
    }

    @Override
    public String postData(ArrayList<NameValuePair> payload, String... data) throws IOException {
        String suffix = "";
        for (String arg : data) {
            suffix += "/" + arg;
        }
        String urlParameters = URLEncodedUtils.format(payload, "UTF-8");
        String key = suffix + urlParameters;

        if (testPostData.containsKey(key)) {
            return testPostData.get(key);
        } else {
            Log.d("droidpile", "Error: data not in testdata. Url: " + key);
            return null;
        }
    }

    private void initialize(Context context) {
        testGetData = new HashMap<String, String>();
        testPostData = new HashMap<String, String>();

        XmlResourceParser xml = context.getResources().getXml(R.xml.testdata);
        int eventType = -1;
        while (eventType != XmlResourceParser.END_DOCUMENT) {
            try {
                eventType = xml.getEventType();
                if (eventType == XmlResourceParser.START_TAG) {
                    String s = xml.getName();

                    if (s.equals("getstring") || s.equals("poststring")) {
                        xml.next();
                        if (xml.getName().equals("key")) {
                            xml.next();
                            String attrName = xml.getText();
                            xml.next();
                            xml.next();
                            if (xml.getName().equals("value")) {
                                xml.next();
                                String contents = xml.getText();

                                if (attrName != null && contents != null && !attrName.isEmpty()
                                        && !contents.isEmpty()) {
                                    if (s.equals("getstring")) {
                                        testGetData.put(attrName, contents);
                                    } else if (s.equals("poststring")) {
                                        testPostData.put(attrName, contents);
                                    }
                                }
                            }
                        }
                    }
                }
                xml.next();
                eventType = xml.getEventType();
            } catch (Exception e) {
                Log.d("droidpile", "Error importing testdata", e);
            }
        }

    }
}