2016-10-26 9 views
0

次のコードを変更しようとしています。これは、今後のAPIの一部としてAndroid環境での使用を強化するためです。AndroidスタジオでJava.text.DateFormatでエラーが発生しました

public class DateFormatter implement JsonDeserializer<Date>, 
     JsonSerializer<Date> { 

private final DateFormat[] formats; 

public DateFormatter() { 
    formats = new DateFormat[3]; 
    formats[0] = new SimpleDateFormat(DATE_FORMAT); 
    formats[1] = new SimpleDateFormat(DATE_FORMAT_V2_1); 
    formats[2] = new SimpleDateFormat(DATE_FORMAT_V2_2); 
    final TimeZone timeZone = TimeZone.getTimeZone("Zulu"); //$NON-NLS-1$ 
    for (DateFormat format : formats) 
     format.setTimeZone(timeZone); 
} 

public Date deserialize(JsonElement json, Type typeOfT, 
     JsonDeserializationContext context) throws JsonParseException { 
    JsonParseException exception = null; 
    final String value = json.getAsString(); 
    for (DateFormat format : formats) 
     try { 
      synchronized (format) { 
       return format.parse(value); 
      } 
     } catch (ParseException e) { 
      exception = new JsonParseException(e); 
     } 
    throw exception; 
} 

public JsonElement serialize(Date date, Type type, 
     JsonSerializationContext context) { 
    final DateFormat primary = formats[0]; 
    String formatted; 
    synchronized (primary) { 
     formatted = primary.format(date); 
    } 
    return new JsonPrimitive(formatted); 
    } 
} 

私はv2.1およびv2.2をサポートする必要はありません。私は配列を削除しようとしていて、ただ一つのインスタンスのためにコード化しています。私はいくつかのエラーに遭遇しています。ここで

は、私がこれまで持っているものです。私はそれをこの点を得れば

class DateFormatter implements JsonDeserializer<Date>, 
    JsonSerializer<Date> { 

private DateFormat formats; 

DateFormatter() { 
    formats = new DateFormat; 
    formats = new SimpleDateFormat(String.valueOf(R.string.date_format), Locale.ENGLISH); 
    final TimeZone timeZone = TimeZone.getTimeZone("Zulu"); 
    for (DateFormat format : formats) 
     format.setTimeZone(timeZone); 

} 

public Date deserialize(JsonElement json, Type typeOfT, 
         JsonDeserializationContext context) throws JsonParseException { 
    JsonParseException exception = null; 
    final String value; 
    value = json.getAsString(); 
    for (DateFormat format : formats) 
     try { 
      synchronized (format) { 
       return format.parse(value); 
      } 
     } catch (ParseException e) { 
      exception = new JsonParseException(e); 
     } 
    throw exception; 
} 

public JsonElement serialize(Date date, Type type, 
          JsonSerializationContext context) { 
    final DateFormat primary; 
    primary = formats; 
    String formatted; 
    synchronized (primary) { 
     formatted = primary.format(date); 
    } 
    return new JsonPrimitive(formatted); 
    } 
} 

しかし、私はエラーを取得します。私が現在心配している主なものはgetStringです。

私はここで間違っていますか?

編集:

@trooper私はプロジェクトをビルドすることはできないですので、私は私が私を反映するために、ポストに第二のコードブロックを変更

--debug --stacktraceを引くことはできません現在のコード。私が変更され;

formats = new SimpleDateFormat(getString(R.string.date_format), Locale.ENGLISH); 

to;

formats = new SimpleDateFormat(String.valueOf(R.string.date_format), Locale.ENGLISH); 

これが私の最初の質問を修正しました。

これで答えました。私の次の質問に移ります。ご覧のとおり、最初のブロックの配列から2番目のブロックの配列に移動します。ライン

for (DateFormat format : formats) 

私は知らない何をすることを削除する方法で、配列に使用されている

私が知っているのforeach「フォーマットはjava.text.DateFormatのには適用されませんforeachの投げている」」をループの一部になって、私が必要とするものを達成してください...これは本当に失われた場所です。

私の最終目標は、API v2 & v3をサポートするEclipseスタジオのJavaで書かれた現在のGitHib APIv3を、Android GitHub API v3に変換して、v2をカバーする必要がないことを確認することです。

私はこの編集が答えるには十分な情報であることを望みます。

+0

あなたはエラーの内容を教えていただけません。あなたの質問を編集し、スタックトレースを追加してください。 – trooper

+0

@trooper私は今日私の仕事に就いています。家に帰るときに質問を編集する必要があります。そして、私はかなり新しく、Androidスタジオでスタックトレースを見つける/取得するにはどうすればいいですか? – Sc4ryb3ar

答えて

0

フォーマットは配列として宣言されていません。これを配列として宣言し、それを1つずつ初期化する必要があります。 これを試してみてください。

プライベートfinal DateFormat [] formats formats = new DateFormat [3]; formats [0] =新しいSimpleDateFormat(getString(R.string.date_format)、Locale.ENGLISH);

ループを削除するには を使用してください。formats = new DateFormat; formats =新しいSimpleDateFormat(String.valueOf(R.string.date_format)、Locale.ENGLISH); final TimeZone timeZone = TimeZone.getTimeZone( "Zulu"); formats.setTimeZone(timeZone);

関連する問題