2017-11-27 4 views
0

Ich muss Datei und POST-Parameter an PHP senden. Ich habe Code von this solution gefunden und kopiert, um eine POST-Anfrage mit Datei und Parametern zu senden, und es funktioniert erfolgreich. Aber wenn ich versuche, Text mit Emoji POST zu senden, dann gibt der Server mir schlechten Emoji-Text zurück.
Beispiel mit Anfrage Daten und Antwort: enter image description here Zuerst dachte ich, dass meine php falsch konfiguriert, aber Getestet habe ich diese Anfrage mit REST-Dienstprogramm und Server die richtige Antwort mit Emojis zurückgegeben.
Dies ist einfach PHP-Code für den Test:Senden von Emojis mit POST mit HttpURLConnection falsche Codierung

$data = $_POST('data'); 
echo $data; 

Was habe ich verpasst? Vielleicht muss ich Charset in Android-Anfrage setzen? aber wo muss ich es platzieren? Oder stimmt etwas mit dem Code in der Lösungsquelle nicht?

Antwort

0

Ich fand eine andere Lösung, funktioniert gut!

  final File uploadFile = new File(file); 
      try { 
       final MultipartUtility http = new MultipartUtility(new URL(url)); 
       http.addFormField("someField", "someValue"); 
       http.addFormField("data", data); 
       http.addFilePart("someFile", uploadFile); 
       String s = http.finish(); 
       Log.d("response", s); 
       return; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

Code:

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 

public class MultipartUtility { 
    private static final String CRLF = "\r\n"; 
    private static final String CHARSET = "UTF-8"; 

private static final int CONNECT_TIMEOUT = 15000; 
private static final int READ_TIMEOUT = 10000; 

private final HttpURLConnection connection; 
private final OutputStream outputStream; 
private final PrintWriter writer; 
private final String boundary; 

private final URL url; 

public MultipartUtility(final URL url) throws IOException { 
    this.url = url; 

    boundary = "---------------------------" + System.currentTimeMillis(); 

    connection = (HttpURLConnection) url.openConnection(); 
    connection.setConnectTimeout(CONNECT_TIMEOUT); 
    connection.setReadTimeout(READ_TIMEOUT); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Accept-Charset", CHARSET); 
    connection.setRequestProperty("Content-Type", 
      "multipart/form-data; boundary=" + boundary); 
    connection.setUseCaches(false); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 

    outputStream = connection.getOutputStream(); 
    writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET), 
      true); 
} 

public void addFormField(final String name, final String value) { 
    writer.append("--").append(boundary).append(CRLF) 
      .append("Content-Disposition: form-data; name=\"").append(name) 
      .append("\"").append(CRLF) 
      .append("Content-Type: text/plain; charset=").append(CHARSET) 
      .append(CRLF).append(CRLF).append(value).append(CRLF); 
} 

public void addFilePart(final String fieldName, final File uploadFile) 
     throws IOException { 
    final String fileName = uploadFile.getName(); 
    writer.append("--").append(boundary).append(CRLF) 
      .append("Content-Disposition: form-data; name=\"") 
      .append(fieldName).append("\"; filename=\"").append(fileName) 
      .append("\"").append(CRLF).append("Content-Type: ") 
      .append(URLConnection.guessContentTypeFromName(fileName)).append(CRLF) 
      .append("Content-Transfer-Encoding: binary").append(CRLF) 
      .append(CRLF); 

    writer.flush(); 
    outputStream.flush(); 
    try { 
     final FileInputStream inputStream = new FileInputStream(uploadFile); 
     final byte[] buffer = new byte[4096]; 
     int bytesRead; 
     while ((bytesRead = inputStream.read(buffer)) != -1) { 
      outputStream.write(buffer, 0, bytesRead); 
     } 
     outputStream.flush(); 
    }finally { 

    } 

    writer.append(CRLF); 
} 

public void addHeaderField(String name, String value) { 
    writer.append(name).append(": ").append(value).append(CRLF); 
} 

public String finish() throws IOException { 
    writer.append(CRLF).append("--").append(boundary).append("--") 
      .append(CRLF); 
    writer.close(); 

    final int status = connection.getResponseCode(); 
    if (status != HttpURLConnection.HTTP_OK) { 
     throw new IOException(String.format("{0} failed with HTTP status: {1}", 
       url, status)); 
    } 

    try { 
     final InputStream is = connection.getInputStream(); 

     InputStreamReader isr = new InputStreamReader(is); 
     BufferedReader in = new BufferedReader(isr); 
     StringBuffer response = new StringBuffer(); 
     String inputLine = in.readLine(); 
     while (inputLine != null) { 
      response.append(inputLine); 
      inputLine = in.readLine(); 
      if (inputLine != null) 
       response.append('\r'); 
     } 
     in.close(); 
     isr.close(); 
     return response.toString(); 

     /* 
     final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     final byte[] buffer = new byte[4096]; 
     int bytesRead; 
     while ((bytesRead = is.read(buffer)) != -1) { 
      bytes.write(buffer, 0, bytesRead); 
     } 
     return bytes.toByteArray(); 
     */ 
    } finally { 
     connection.disconnect(); 
    } 
} 
} 
Verwandte Themen