2

我的 Android 应用中有一个可用的日历。我的目标是从与我的另一个 Google 帐户关联的日历中向此日历添加事件。我发现为了让应用程序看到事件,我必须公开 Google 日历,这样不仅我的应用程序,而且每个人都可以看到日历。我知道 Google 日历设置正确,因为我可以从中看到 JSON 数据。解析 JSON 数据的 URL 是:http://www.google.com/calendar/feeds/[my other calendar]@gmail.com/public/full. 所以我的问题是如何将这些 JSON 数据解析到我的应用程序中?我想我必须创建一个 JSON 解析类,然后在我的日历活动中引用该 JSON 解析类。这是我的日历类的样子:`

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.content.Intent;

public class Calendar32View extends Activity {

public GregorianCalendar month, itemmonth;// calendar instances.

public Calendar31Adapter adapter;// adapter instance
public Handler handler;// for grabbing some event values for showing the dot
                        // marker.
public ArrayList<String> items; // container to store calendar items which
                                // needs showing the    event marker
ArrayList<String> event;
LinearLayout rLayout;
ArrayList<String> date;
ArrayList<String> desc;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cal3_2);
    Locale.setDefault(Locale.US);

    rLayout = (LinearLayout) findViewById(R.id.text);
    month = (GregorianCalendar) GregorianCalendar.getInstance();
    itemmonth = (GregorianCalendar) month.clone();

    items = new ArrayList<String>();

    adapter = new Calendar31Adapter(this, month);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(adapter);
    handler = new Handler();

    // ---
    //handler.post(calendarUpdater);//--- adds Events
    // ----

    TextView title = (TextView) findViewById(R.id.title);
    title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));

    // --- Previous Month Button
    RelativeLayout previous = (RelativeLayout) findViewById(R.id.previous);
    previous.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            setPreviousMonth();
            refreshCalendar();
        }
    });
    // --- END Previous Month Button

    // --- Next Month Button
    RelativeLayout next = (RelativeLayout) findViewById(R.id.next);
    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            setNextMonth();
            refreshCalendar();

        }
    });
    // --- END Next Month Button

    // --- onClick of date ----

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // removing the previous view if added
            if (((LinearLayout) rLayout).getChildCount() > 0) {
                ((LinearLayout) rLayout).removeAllViews();
            }
        }

    });

    // --- END onClick of date ----

}// --- END onCreate

// ---- METHODS ---

protected void setPreviousMonth() {
    if (month.get(GregorianCalendar.MONTH) == month
            .getActualMinimum(GregorianCalendar.MONTH)) {
        month.set((month.get(GregorianCalendar.YEAR) - 1),
                month.getActualMaximum(GregorianCalendar.MONTH), 1);
    } else {
        month.set(GregorianCalendar.MONTH,
                month.get(GregorianCalendar.MONTH) - 1);
    }

}// --- end method

protected void setNextMonth() {
    if (month.get(GregorianCalendar.MONTH) == month
            .getActualMaximum(GregorianCalendar.MONTH)) {
        month.set((month.get(GregorianCalendar.YEAR) + 1),
                month.getActualMinimum(GregorianCalendar.MONTH), 1);
    } else {
        month.set(GregorianCalendar.MONTH,
                month.get(GregorianCalendar.MONTH) + 1);
    }

}// --- END Method

protected void showToast(String string) {
    Toast.makeText(this, string, Toast.LENGTH_SHORT).show();

}// --- end method

public void refreshCalendar() {
    TextView title = (TextView) findViewById(R.id.title);

    adapter.refreshDays();
    adapter.notifyDataSetChanged();

    title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
}// --- end method



// --- END METHODS -----

}`

我似乎找不到任何关于如何做到这一点的教程。我的目标是 SDK 8,以便它可以在我的 2.3.4 手机上运行。

4

1 回答 1

0

Java 包含一个 JSONObject 类:cf:" http://www.json.org/javadoc/org/json/JSONObject.html " U 可以使用这些方法中的任何一个,特别是 getString(String key),例如使用 hashMap

于 2013-11-19T06:15:59.153 回答