从Unix进行Android JSON时间转换

时间:2012-03-09 19:48:13

标签: android json unix simpledateformat

我之前提出过这个问题并获得了一些很好的反馈,但我仍然无法让我的程序正常运行。基础知识:它是一个调度应用程序。我试图将我的JSON提要中的字段转换为人类可读的时间/日期并显示在我的ListView适配器中。在下面的代码中,我将引入一个名为“ShowDate”的字符串,其中包含Unix时间戳。现在(在我添加SimpleDateFormat之前)该字符串显示Unix代码。当我添加SimpleDateFormat时,我的活动在加载时崩溃。我想我只是有一些错误的标签或错误的位置。任何帮助表示赞赏。我已经包含了我的活动代码和我收到的logcat错误。

public class Schedule extends ListActivity {

protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected TextView textView;
protected ImageView imageView;
protected ArrayList<HashMap<String, String>> myList;

Date F = new Date("showdate"); String formattedDate = new SimpleDateFormat("mm/dd/yy hh:mm:ss").format(F);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.schedule);



    View layout = findViewById(R.id.gradiant);

    GradientDrawable gd = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM, new int[] {
                    0xFF95B9C7, 0xFF06357A });
    gd.setCornerRadius(2f);

    layout.setBackgroundDrawable(gd);

    myList = new ArrayList<HashMap<String, String>>();

    JSONObject jsonObjSend = new JSONObject();
    JSONObject json = JSONfunctions
            .getJSONfromURL("myAPIURL");


        JSONArray current = json.getJSONArray("d");


        for (int i = 0; i < current.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject e = current.getJSONObject(i);

            map.put("id", String.valueOf(i));
            map.put("showid", "" + Html.fromHtml(e.getString("ShowID")));
            map.put("name", "" + Html.fromHtml(e.getString("Title")));
            map.put("showvenue", "" + e.getString("ShowVenue"));
            map.put("subtutle", "" + e.getString("SubTitle"));
            map.put("venueid", "" + e.getString("VenueID"));
            map.put("showdate", "" + e.getString("ShowDate"));
            map.put("showstart", "" + e.getString("ShowStart"));
            map.put("showend", "" + e.getString("ShowEnd"));
            map.put("gatesopen", "" + e.getString("GatesOpen"));
            map.put("aboutartist", "" + e.getString("AboutArtist"));
            map.put("program", "" + Html.fromHtml(e.getString("Program")));
            map.put("programnotes", "" + Html.fromHtml(e.getString("ProgramNotes")));
            map.put("pricing", "" + e.getString("Pricing"));
            map.put("featuring", "" + e.getString("Featuring"));
            map.put("parkdetails", "" + e.getString("ParkDetails"));
            map.put("starred", "" + e.getString("Starred"));
            map.put("image500", "" + e.getString("Image500"));
            map.put("image250", "" + e.getString("Image250"));
            map.put("aboutartist", "" + Html.fromHtml(e.getString("AboutArtist")));


            myList.add(map);
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());

    }



    ListAdapter adapter = new SimpleAdapter(this, myList,R.layout.line_item, 
            new String[] { "name", "showdate","showstart", "showvenue", "image250" }, 
            new int[] { R.id.title, R.id.showdate,R.id.showstart,R.id.showvenue, R.id.list_image });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            HashMap<String, String> hashMap = myList.get(position);
            // hashMap.put("map", hashMap);
            Intent intent = new Intent(getApplicationContext(),
                    ArtistDetails.class);
            intent.putExtra("map", hashMap);
            startActivity(intent);

        }

    });
}

日志:

 03-09 13:36:29.271: D/dalvikvm(3239): GC_EXTERNAL_ALLOC freed 61K, 53% free 2578K/5379K, external 1625K/2137K, paused 44ms
03-09 13:36:31.191: D/dalvikvm(3239): GC_EXTERNAL_ALLOC freed 5K, 52% free 2597K/5379K, external 2881K/3528K, paused 48ms
03-09 13:36:31.571: D/dalvikvm(3239): GC_EXTERNAL_ALLOC freed 2K, 52% free 2597K/5379K, external 4811K/6009K, paused 36ms
03-09 13:36:31.892: D/dalvikvm(3239): GC_EXTERNAL_ALLOC freed <1K, 52% free 2599K/5379K, external 6741K/8419K, paused 42ms
03-09 13:36:32.601: D/dalvikvm(3239): GC_EXTERNAL_ALLOC freed <1K, 52% free 2603K/5379K, external 10601K/10719K, paused 36ms
03-09 13:36:35.951: D/AndroidRuntime(3239): Shutting down VM
03-09 13:36:35.951: W/dalvikvm(3239): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-09 13:36:35.972: E/AndroidRuntime(3239): FATAL EXCEPTION: main
03-09 13:36:35.972: E/AndroidRuntime(3239): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aqdct.ravinia2012v11/com.aqdct.ravinia2012v11.Schedule}: java.lang.IllegalArgumentException
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.os.Looper.loop(Looper.java:123)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread.main(ActivityThread.java:3647)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.lang.reflect.Method.invokeNative(Native Method)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.lang.reflect.Method.invoke(Method.java:507)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at dalvik.system.NativeStart.main(Native Method)
03-09 13:36:35.972: E/AndroidRuntime(3239): Caused by: java.lang.IllegalArgumentException
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.util.Date.parse(Date.java:505)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.util.Date.<init>(Date.java:148)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at com.aqdct.ravinia2012v11.Schedule.<init>(Schedule.java:39)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.lang.Class.newInstanceImpl(Native Method)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at java.lang.Class.newInstance(Class.java:1409)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-09 13:36:35.972: E/AndroidRuntime(3239):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)
03-09 13:36:35.972: E/AndroidRuntime(3239):     ... 11 more
03-09 13:36:39.331: I/Process(3239): Sending signal. PID: 3239 SIG: 9

如果有任何其他信息可以帮助我们开展此工作,请告诉我。

1 个答案:

答案 0 :(得分:1)

日期(字符串)为depreciated

相反,你应该使用DateFormat.parse(String s)

除此之外,你为什么要过去&#34; showdate&#34;到下面的Date构造函数?你想要传递一些实际的时间,否则它不会解析任何东西。

Date F = new Date("showdate"); String formattedDate = new SimpleDateFormat("mm/dd/yy hh:mm:ss").format(F);