不同的Android手机使用不同的编码?

时间:2015-08-06 07:25:13

标签: java android http http-get

我正在开发一款应用程序播放歌曲并与服务器通信。方法为addMusicdeleteMusicrequestMusicList。他们都使用http GET方法。

以下案例

deleteMusicrequestMusicList适用于所有手机。除了addMusic,它只适用于某些手机。

addMusic仅适用于三星手机。而在nuu,mi和meizu。这些是我们在这里的手机,所以我们只测试了那些手机的应用程序。

我的问题:

由于addMusic url字符串中包含的参数包含中文字符,我认为这是问题的原因。这是日志。

08-06 15:00:22.102: V/HttpConnectionManager(13211): Get url string is http://115.28.6.88:7100/cgi-bin/v1/music_add?content_url=http://fdfs.xmcdn.com/group12/M07/03/65/wKgDW1VQa-6iJ2e1AB7JjcEKs2E684.mp3&cover_image_url=http://fdfs.xmcdn.com/group11/M02/03/7B/wKgDa1VQbA2j2mOpAAiTLl27BzM875_mobile_small.jpg&dev_id=test101&duration=252&timestamp=1438844422&title=《Couldyoustaywithme》张靓颖(电视剧《洋嫁》主题曲)&type=type_habit&sign=f834a4667004cebdac1516584b4a4930

08-06 15:00:22.272: I/Adreno200-EGLSUB(13211): <ConfigWindowMatch:2252>: Format RGBA_8888.

08-06 15:00:22.342: V/HttpConnectionManager(13211): Response code from GET: 200

08-06 15:00:22.352: V/DetailManager(13211): Handled http response is {"retcode": 100004, "retinfo": "check sign fail, request_sign:[F834A4667004CEBDAC1516584B4A4930], mysign:[EBFA4D2D40F20DF1B124375A0D9FAD38], mysign_src_nokey:[content_url=http://fdfs.xmcdn.com/group12/M07/03/65/wKgDW1VQa-6iJ2e1AB7JjcEKs2E684.mp3&cover_image_url=http://fdfs.xmcdn.com/group11/M02/03/7B/wKgDa1VQbA2j2mOpAAiTLl27BzM875_mobile_small.jpg&dev_id=test101&duration=252&timestamp=1438844422&title=?Couldyoustaywithme????????????????&type=type_habit]"}

08-06 15:00:22.352: V/AudioFragment(13211): Server request failed: check sign fail, request_sign:[F834A4667004CEBDAC1516584B4A4930], mysign:[EBFA4D2D40F20DF1B124375A0D9FAD38], mysign_src_nokey:[content_url=http://fdfs.xmcdn.com/group12/M07/03/65/wKgDW1VQa-6iJ2e1AB7JjcEKs2E684.mp3&cover_image_url=http://fdfs.xmcdn.com/group11/M02/03/7B/wKgDa1VQbA2j2mOpAAiTLl27BzM875_mobile_small.jpg&dev_id=test101&duration=252&timestamp=1438844422&title=?Couldyoustaywithme????????????????&type=type_habit]

从第一行可以看出,这包含我发送的URL。该网址包含这些&#34;张靓颖(电视剧“洋嫁”主题曲&#34;标题参数下的汉字。当服务器返回响应时,这些字符变为&#34; ?????????????&#34;。

请注意,这个问题只发生在上述Android手机(mi,meizu和nuu)中。对于三星,此从不发生。

以下是我的addMusic方法的代码:

public void addMusic(final Music music)
{
    new Thread(new Runnable() 
    {   
        @Override
        public void run() 
        {
            String contentURL = KEY_CONTENT_URL + "=" + music.mContentURL;
            String coverImageURLData = "&" + KEY_COVER_IMAGE_URL + "=" + music.mCoverImageURL;
            String deviceIDData = "&" + KEY_DEVICE_ID + "=" + mDeviceID;
            String duration = "&" + KEY_DURATION + "=" + Long.toString(music.mDuration);
            String timestamp = "&" + KEY_TIMESTAMP + "=" + Long.toString(System.currentTimeMillis()/1000);
            String  titleData = "&" + KEY_TITLE + "=" + music.mTitle.replaceAll("\\s", "");
            String typeData = "&" + KEY_TYPE + "=" + music.mType;

            String dataArrayString = contentURL + coverImageURLData + deviceIDData + duration + timestamp + titleData + typeData;

            String key = "&" + KEY_KEY + "=" + KEY;
            String sign = "&" + KEY_SIGN + "=" + Utilities.getMD5String(dataArrayString + key);

            String parametersURL = dataArrayString + sign;

            try 
            {
                handleHttpResponse(mConnectionManager.get(URL_MAIN + URL_ADD_MUSIC + parametersURL), REQUEST_CODE_ADD_MUSIC);
            } 

            catch (IOException e) 
            {
                e.printStackTrace();

                mListener.onServerRequestFail(e.getLocalizedMessage());
            }
        }
    }).start();
}

以下是Http GET的代码:

public String get(String urlString) throws IOException
    {
        Log.v(LOG_TAG, "Get url string is " + urlString);

        URL url = new URL(urlString);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = connection.getResponseCode();

        Log.v(LOG_TAG, "Response code from GET: " + responseCode);

        if(responseCode == RESPONSE_OK)
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            StringBuffer response = new StringBuffer();
            String inputLine;

            while ((inputLine = reader.readLine()) != null) 
            {
                response.append(inputLine);
            }
            reader.close();

            return response.toString();
        }

        else
        {
            return ERROR_NO_RESPONSE;
        }
    }
  • 我尝试检查defaultCharset,这是&#34; UTF-8&#34;适用于所有经过测试的手机。

我该如何解决这个问题?请帮忙。非常感谢

------------尝试过URLEncoder.encode(music.mTitle.replaceAll(&#34; \ s&#34;,&#34;&#34;),&#34; UTF-8&#34;)----------------

这是Log:

08-06 15:53:08.852: V/HttpConnectionManager(15061): Get url string is http://115.28.6.88:7100/cgi-bin/v1/music_add?content_url=http://fdfs.xmcdn.com/group6/M09/1E/0B/wKgDg1UrI8iw-rGzAAyaxOKiDrc706.mp3&cover_image_url=http://fdfs.xmcdn.com/group6/M00/1E/18/wKgDg1UrJp7TMEMMAAP5a37TPkE290_mobile_small.jpg&dev_id=test101&duration=103&timestamp=1438847588&title=%E3%80%8Ayouaremysunshine%E3%80%8B%EF%BC%8D%E5%BC%A0%E9%9D%93%E9%A2%96%EF%BC%88%E7%94%B5%E5%BD%B1%E3%80%8A%E4%BD%95%E4%BB%A5%E7%AC%99%E7%AE%AB%E9%BB%98%E3%80%8B%E8%8B%B1%E6%96%87%E6%8F%92%E6%9B%B2&type=type_habit&sign=f7b8c5a0cb1fceb32a54b428311744ad
08-06 15:53:08.902: I/Adreno200-EGLSUB(15061): <ConfigWindowMatch:2252>: Format RGBA_8888.
08-06 15:53:19.692: V/HttpConnectionManager(15061): Response code from GET: 200
08-06 15:53:19.692: V/DetailManager(15061): Handled http response is {"retcode": 100004, "retinfo": "check sign fail, request_sign:[F7B8C5A0CB1FCEB32A54B428311744AD], mysign:[537721D351DF6C8B40C62C4F8672EADF], mysign_src_nokey:[content_url=http://fdfs.xmcdn.com/group6/M09/1E/0B/wKgDg1UrI8iw-rGzAAyaxOKiDrc706.mp3&cover_image_url=http://fdfs.xmcdn.com/group6/M00/1E/18/wKgDg1UrJp7TMEMMAAP5a37TPkE290_mobile_small.jpg&dev_id=test101&duration=103&timestamp=1438847588&title=\u300ayouaremysunshine\u300b\uff0d\u5f20\u9753\u9896\uff08\u7535\u5f71\u300a\u4f55\u4ee5\u7b19\u7bab\u9ed8\u300b\u82f1\u6587\u63d2\u66f2&type=type_habit]"}
08-06 15:53:19.702: V/AudioFragment(15061): Server request failed: check sign fail, request_sign:[F7B8C5A0CB1FCEB32A54B428311744AD], mysign:[537721D351DF6C8B40C62C4F8672EADF], mysign_src_nokey:[content_url=http://fdfs.xmcdn.com/group6/M09/1E/0B/wKgDg1UrI8iw-rGzAAyaxOKiDrc706.mp3&cover_image_url=http://fdfs.xmcdn.com/group6/M00/1E/18/wKgDg1UrJp7TMEMMAAP5a37TPkE290_mobile_small.jpg&dev_id=test101&duration=103&timestamp=1438847588&title=《youaremysunshine》-张靓颖(电影《何以笙箫默》英文插曲&type=type_habit]

1 个答案:

答案 0 :(得分:0)

我解决了。感谢@Andy Turner和@ lorenzo-s的评论提示。

我的错误是我在获取MD5校验和之前将字符串编码为UTF-8。我注意到当我将请求(URLEncoded)发送到服务器时,包含标题的响应是正确的。该错误仅与MD5签名有关。

正确的方法是首先获取MD5校验和,而不编码title参数。然后在将title参数附加到URL String之前,将其转换为UTF-8。

感谢@Andy Turner提醒我,我必须处理&#34;&amp;&#34;在参数中。