Java Http请求“;获得;

Java Http请求“;获得;,java,android,Java,Android,我有一个类,它向服务器发送Http请求。 在这个类中,我创建了一个时间戳。小时、分钟的变量。。。正确,并用正确的变量填充。但是当我创建timestamp变量时,它给出了一个exeption,但是消息没有出现在log cat中,我不知道为什么 我的代码: public String GetData(String reqUrl) { String response = null; try { String tag [] = {"Sat", "Sun", "M

我有一个类,它向服务器发送Http请求。 在这个类中,我创建了一个时间戳。小时、分钟的变量。。。正确,并用正确的变量填充。但是当我创建timestamp变量时,它给出了一个exeption,但是消息没有出现在log cat中,我不知道为什么

我的代码:

    public String GetData(String reqUrl) {
    String response = null;
    try {

        String tag [] = {"Sat", "Sun", "Mon", "tue", "Wed", "Thu", "Fri"};
        String monat [] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};

        Calendar cal = new GregorianCalendar();
        cal.setTime( new Date() );

        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        int year = cal.get(Calendar.YEAR);
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
        int month = cal.get(Calendar.MONTH);
        int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);

        String timestamp = tag[dayOfWeek] + ", " + dayOfMonth + " " + monat[month] + " " + year + " " + hourOfDay + ":" + minute + ":" + second + " " + "GMT";

        String timeAndAppID = HttpHandler.LoginParams.appid + timestamp;

        String hash = getMD5EncryptedString(timeAndAppID);

        URL url = new URL(reqUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Pragma", "no-cache");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Cache-Control", "no-cache");
        conn.setRequestProperty("WWSVC-TS", timestamp);
        conn.setRequestProperty("WWSVC-HASH", hash);

        conn.connect();

        // read the response
        InputStream in = new BufferedInputStream(conn.getInputStream());
        response = convertStreamToString(in);
    } catch (MalformedURLException e) {
        Log.e(TAG, "MalformedURLException: " + e.getMessage());
    } catch (ProtocolException e) {
        Log.e(TAG, "ProtocolException: " + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "IOException: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
    return response;
}
tl;博士 2008年6月3日星期二11:05:30 GMT

细节 我们无法解决您的确切问题,因为您在被要求两次后尚未提供异常信息

我可以缩短并更正您的日期时间代码。问题中的代码将日期时间误报为GMT/UTC,而事实上它位于JVM的当前默认时区中,由
gregoriacalendar
类隐式应用

您使用的是麻烦的旧日期时间类,它们现在是遗留的,被java.time类取代

要以UTC(GMT)为单位获取当前时刻,只需请求一个
瞬时值
。该类表示时间线上的一个时刻,分辨率为(小数点的九(9)位)

要在系统之间传递日期时间值,应使用标准格式。time类在生成和解析字符串时使用这些标准格式

String output = instant.toString() ;
2017-03-26T00:25:07.521Z

如果您坚持使用特定格式,请让
DateTimeFormatter
类来生成字符串。无需指定格式模式,因为您的格式与RFC 1123/RFC 822定义的格式相同。那个格式化程序已经被激活了。但是请注意,您的代码并没有以与RFC相同的方式缩写月份名称

最后,我要重复我自己的话:这是一种糟糕的格式,因为它很难阅读,很难解析,并且假设是英语——我建议您改用ISO8601格式。现代协议和标准已经放弃了这些旧的RFC格式,转而使用ISO 8601

DateTimeFormatter f = DateTimeFormatter.RFC_1123_DATE_TIME ; 
Instant
类是java.time中的基本构建块类。有关更多功能(如格式),请使用常量转换为对象

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );
String output = odt.format( f );

关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

从哪里获得java.time类

  • 后来
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 该项目专门为Android采用了ThreeTen Backport(如上所述)

该项目使用其他类扩展了java.time。这个项目是java.time将来可能添加的一个试验场。您可以在这里找到一些有用的类,如、、和。

显示您的例外情况您是否查看了日期格式?看起来您正在手动执行许多不必要的操作。是的,我以前查看过:/n您已经被要求发布异常。请这样做。
日历
隐式应用JVM的当前默认时区。所以您将时间戳报告为GMT(UTC)是错误的、不正确的数据。
Instant instant = Instant.parse( "2017-03-26T00:25:07.521Z" );
DateTimeFormatter f = DateTimeFormatter.RFC_1123_DATE_TIME ; 
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );
String output = odt.format( f );