Http(s) request responds with garbage characters

时间:2018-09-22 23:05:04

标签: java android android-studio

I am trying to send a request to a website to get some data from it. However, the response is garbage characters like:

Ì 3ú¢¸<N¤@H±ü>§#Fe®¡+K·fÐcıaOqâò;'Êù!°â<rº\¼YDóß1K`òúüb¨ÑTcíÆ

This only happens for reposes with data in it, and errors also work fine. The response is supposed to look like this (from browser request): {"d":[{"__type":"CalendarTransport:http:...","activityId":2662,"activityImportIdentifier":null,"activityType":1,"allDay":false,"attendanceMode":1,....

Here is my code

        try
        {
            HttpsURLConnection httpsURLConnection;
            httpsURLConnection = (HttpsURLConnection) new URL("https", redacted, redacted).openConnection();

            try
            {
                httpsURLConnection.setRequestMethod("POST");
            }
            catch (Exception e)
            {
                Log.d(TAG, "doInBackground: oh no (3) it's a " + e.toString());
            }
            httpsURLConnection.setRequestProperty("Accept-Charset", "*/*");
            httpsURLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            httpsURLConnection.setRequestProperty("Connection", "keep-alive");
            httpsURLConnection.setRequestProperty("Content-Type", "application/json");
            httpsURLConnection.setRequestProperty("Cookie", new DomainCookies(redacted).toRequestHeader());
            httpsURLConnection.setDoOutput(true);
            httpsURLConnection.setDoInput(true);

            OutputStream os = httpsURLConnection.getOutputStream();

            OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
            osw.write(redacted);
            osw.flush();
            osw.close();
            os.close(); 

            StringBuilder mssg = new StringBuilder();
            DataInputStream inputStream;

            int status = httpsURLConnection.getResponseCode();

            if (status != HttpsURLConnection.HTTP_OK)
            {
                inputStream = new DataInputStream(httpsURLConnection.getErrorStream());
                Log.d(TAG, "doInBackground: HTTP error");
            }
            else
            {
                inputStream = new DataInputStream(httpsURLConnection.getInputStream());
                Log.d(TAG, "doInBackground: HTTP OK");
            }
            try
            {
                for(int c = inputStream.read(); inputStream.available() != 0; c = inputStream.read())
                {
                    mssg.append((char)c);
                }

                Log.d(TAG, "doInBackground: " + mssg);
            }
            catch (Exception e)
            {
                Log.d(TAG, "doInBackground: oh no (1) it's a " + e.toString());
            }
        }
        catch (MalformedURLException e)
        {
            Log.d(TAG, "onCreate: Oh no it's a " + e.toString());

        }
        catch (Exception e)
        {
            Log.d(TAG, "doInBackground: Oh no it's a " + e.toString());
        }

The browser can get the response fine, do it must be something I am doing (or android, but I doubt that)

1 个答案:

答案 0 :(得分:1)

我将从摆脱这一行开始:

void RepeatingWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.fillRect(event->rect(), QBrush(Qt::white));
    painter.setFont(displayFont);
    QFontMetrics fontMetrics(displayFont);
    int i = std::max(event->rect().top()/itemHeight, 0);
    int j = std::min(event->rect().bottom()/itemHeight+1, displayItems.size());
    QRect itemRect(0, i*itemHeight, width(), itemHeight);
    for(; i < j; i++){
        painter.setPen(QPen(Qt::gray));
        painter.drawRect(itemRect);
        painter.setPen(QPen(Qt::black));
        painter.drawText(8, 4 + itemRect.top() + fontMetrics.ascent(), displayItems[i].name);
        itemRect.translate(0, itemHeight);
    }
}

您的代码未处理这些形式的数据编码,因此不要要求服务器以这种方式对数据进行编码。

除此之外,现在是2018年。停止使用httpsURLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate, br"); 。使用更现代,更不易出错的工具,例如OkHttp