-2

I have a Class, what send a Http Request to a Server. In this class i create a timestamp. The variable for hour, minute... is correct and fill with the correct variable. But when i create my timestamp variable it gives an exeption, but the massage come not in the log cat and i dont know why.

My Code:

    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;
}
Max
  • 9
  • 1
  • 6

1 Answers1

0

tl;dr

OffsetDateTime.now( ZoneOffset.UTC )
              .format( DateTimeFormatter.RFC_1123_DATE_TIME )

Tue, 3 Jun 2008 11:05:30 GMT

Details

We cannot solve your exact problem as you have not yet provided the Exception information after being asked twice for it.

I can shorten, and correct, your date-time code. The code in the Question misreports a date-time as being GMT/UTC when in fact it is in the JVM’s current default time zone that is implicitly applied by the GregorianCalendar class.

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

To get the current moment in UTC (GMT), simply ask for an Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.now();

For communicating date-time values between systems, you should be using standard ISO 8601 formats. The java.time classes use these standard formats when generating and parsing strings.

String output = instant.toString() ;

2017-03-26T00:25:07.521Z

Instant instant = Instant.parse( "2017-03-26T00:25:07.521Z" );

If you insist on using your particular format instead, let DateTimeFormatter class do the work of generating the string. No need to specify a formatting pattern, as your format appears to that defined by RFC 1123 / RFC 822. That formatter is already pre-defined in java.time. But note that your code does not abbreviate the month names in the same manner as the RFC.

Lastly, I will repeat myself: This is a terrible format as it is difficult to read, difficult to parse, and assumes English language – I urge you to use ISO 8601 formats instead. Modern protocols and standards have abandoned these old RFCs’ format to use ISO 8601 instead.

DateTimeFormatter f = DateTimeFormatter.RFC_1123_DATE_TIME ; 

The Instant class a basic building block class in java.time. For more features such as formatting, convert to an OffsetDateTime object using the ZoneOffset.UTC constant.

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

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154