The problem is that I send JSON to the database, and there instead of Cyrillic question marks.

I type like this:

 ArrayList<String> arrayList = new ArrayList<>(); arraylist.add("first"); arraylist.add("second"); arraylist.add("third"); arraylist.add("fourth"); 

Then I convert ArrayList to Json

 String jsonString = new Gson().toJson(arrayList); 

Sending:

 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(Application.TEST_API); List<NameValuePair> valuePairs = new ArrayList<>(); valuePairs.add(new BasicNameValuePair("request", "SAVE_RESULT")); valuePairs.add(new BasicNameValuePair("result", jsonString)); httppost.setEntity(new UrlEncodedFormEntity(valuePairs)); HttpResponse httpResponse = httpclient.execute(httppost); 

Question:

1) How to jsonString my array to UTF-8 in jsonString ?

2) Or a problem when sending, where httppost.setEntity(new UrlEncodedFormEntity(valuePairs)) , since, UrlEncodedFormEntity()

 public UrlEncodedFormEntity(List<? extends NameValuePair> parameters) throws UnsupportedEncodingException { this(parameters, "ISO-8859-1"); } 

    1 answer 1

    I highly recommend using ready-made solutions - for example, Retrofit (de facto standard for working with web services).

    For example, they do this using jSonWriter: https://github.com/square/retrofit/blob/master/retrofit-converters/gson/src/main/java/retrofit2/converter/gson/GsonRequestBodyConverter.java

      @Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } 
    • While there is no time to study. What do you think about my question? - DevOma