1

私はこのコードで 4 時間戦っています。おそらく何か間違ったことをしているのでしょう。「テキスト」は「????」のようにサーバーに送信されます。

私はログをチェックしました:

error_log(mb_detect_encoding($_POST['text']))

それは私にASCIIを与えます。

   /**
     * Send the question to the server
     */
    private void sendQuestion() {
        final String uid = getContext().getSharedPreferences(LoginActivity.PREFS_NAME, 0).getString("uid", "0");
        final String text = ((EditText) findViewById(R.id.question_et)).getText().toString();

        final MultipartEntity multipartEntity = new MultipartEntity();

        if (mFile != null) {
            final FileBody fileBody = new FileBody(mFile);
            multipartEntity.addPart("userfile", fileBody);
        }

        try {
            multipartEntity.addPart("uid", new StringBody(uid));
            multipartEntity.addPart("type", new StringBody(mFile == null ? "1" : "2"));
            multipartEntity.addPart("category", new StringBody(String.valueOf(mCategoryId + 1)));
            multipartEntity.addPart("text", new StringBody(URLEncoder.encode(text, "UTF-8")));

        } catch (UnsupportedEncodingException e) {
        }

        final HttpParams httpParameters = new BasicHttpParams();
        HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
        HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);

        final HttpPost httpPost = new HttpPost("http://site.ru/add.php");
        httpPost.setEntity(multipartEntity);

        final HttpClient httpClient = new DefaultHttpClient(httpParameters);

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

                Toast.makeText(getContext(), "Ваш вопрос отправлен, ждите ответа", Toast.LENGTH_LONG).show();
            }

            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    final HttpResponse httpResponse = httpClient.execute(httpPost);
                    final int code = httpResponse.getStatusLine().getStatusCode();
                    final String response = EntityUtils.toString(httpResponse.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return null;
            }

        }.execute();
    }
4

1 に答える 1

1

ここに正しい答えがあります(コメントごとに:))

StringBody オブジェクトにエンコーディングを直接設定してみてください。

StringBody(String text, Charset charset)代わりに使うことで

エンコーディングを設定しないと、US-ASCII charsetが使用されますが、これは望んだものではありません。

Stringbody コンストラクターの JavaDoc

于 2013-04-07T16:48:32.790 に答える