cn.edu.szjm.service.PostRecordTask.java Source code

Java tutorial

Introduction

Here is the source code for cn.edu.szjm.service.PostRecordTask.java

Source

/*
 * 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 cn.edu.szjm.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;

import com.kaixin.connect.Kaixin;
import com.kaixin.connect.Util;
import com.kaixin.connect.exception.KaixinError;

public class PostRecordTask extends AsyncTask<Object, Void, Integer> {
    private static final String RESTAPI_INTERFACE_POSTRECORD = "/records/add.json";
    private Activity activity;
    private InputStream in;
    private String news;
    private KaixinRequestListener requestListener;

    public PostRecordTask(Activity activity, InputStream in, String news, KaixinRequestListener requestListener) {
        this.activity = activity;
        this.in = in;
        this.news = news;
        this.requestListener = requestListener;
    }

    protected Integer doInBackground(Object... params) {

        requestListener.onRequestStart();

        if ((activity == null) || (!(in == null) && (news == null)))
            requestListener.onRequestFault(new Throwable(new NullPointerException().getMessage()));

        Kaixin kaixin = Kaixin.getInstance();

        try {

            Bundle bundle = new Bundle();
            bundle.putByteArray("content", news.getBytes());

            Map<String, Object> photoes = new HashMap<String, Object>();
            photoes.put("filename", in);

            String jsonResult = kaixin.uploadContent(activity, RESTAPI_INTERFACE_POSTRECORD, bundle, photoes);

            if (TextUtils.isEmpty(jsonResult)) {

                requestListener.onRequestFault(new KaixinError(Constant.RESULT_FAILED_REQUEST_ERR,
                        "/", "", ""));

            } else {

                KaixinError kaixinError = Util.parseRequestError(jsonResult);

                if (kaixinError != null) {

                    requestListener.onRequestError(kaixinError);

                } else {

                    long rid = getRecordID(jsonResult);

                    if (rid < 0)
                        requestListener.onRequestError(new KaixinError(Constant.RESULT_POST_RECORD_FAILED,
                                "/???", "", jsonResult));
                    else
                        requestListener.onRequestComplete(jsonResult);
                }

            }

        } catch (IOException e) {
            requestListener
                    .onRequestFault(new KaixinError(Constant.RESULT_FAILED_NETWORK_ERR, e.getMessage(), "", ""));
        } catch (JSONException e) {
            requestListener
                    .onRequestError(new KaixinError(Constant.RESULT_FAILED_JSON_PARSE_ERR, e.getMessage(), "", ""));
        }
        return params.length;
    }

    private long getRecordID(String jsonResult) throws JSONException {

        JSONObject jsonObj = new JSONObject(jsonResult);

        if (jsonObj == null) {
            return 0;
        }

        long rid = jsonObj.optInt("rid");

        return rid;
    }

}