从Unix的Andr​​oid JSON时间转换 [英] Android JSON time conversion from Unix

查看:142
本文介绍了从Unix的Andr​​oid JSON时间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我前面问这个问题,并得到了一些很好的意见,但我还是没能得到我的程序运行正常。基础知识:它的调度应用。我试图让一个字段在我的JSON饲料转换成在我的ListView适配器人类可读的时间/日期和显示。在code下面我拉在一个名为包含Unix时间戳showdate相​​似字符串。现在(我才加的SimpleDateFormat)字符串显示了Unix code。当我加入的SimpleDateFormat我的活动崩溃的负载。我想我只是有一些错误标记或在错误的位置。任何帮助是AP preciated。我已经包含了code为我的行为和错误logcat中我收到。

I asked this question earlier and got some great feedback, but I still wasn't able to get my program running correctly. The Basics: Its a scheduling app. I am trying to get a field in my JSON feed to be converted into a human readable time/date and display in my ListView adaptor. In the code below I am pulling in a string called "ShowDate" that contains the Unix timestamp. Right now (before I added SimpleDateFormat) the string displays the Unix code. When I added the SimpleDateFormat my activity crashes on load. I think I just have something mis-labeled or in the wrong position. Any help is appreciated. I've included the code for my activity and the logcat error I receive.

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

请让我知道是否有帮助得到这个工作所需要的任何其他信息。

Please let me know if there is any other information needed to help get this working.

推荐答案

日期(字符串)是的德preciated

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

Instead you should use DateFormat.parse(String s)

这一边,你为什么要通过showdate相​​似下面的日期构造函数?你会希望在某些实际时间,否则它不会解析任何通过。

That aside, why are you passing "showdate" to the Date constructor below? You'd want to pass in some actual time otherwise it's not going to parse anything.

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

这篇关于从Unix的Andr​​oid JSON时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆