0

Android でマルチパート データをアップロードしようとしていますが、コードが機能しません。私のコードは以下です。私のアップロード情報はほとんどがテキスト フィールドです。そのため、キーにフィールド名が含まれ、値にフィールド値が含まれるように HashMap を保持しました。しかし、私の解決策はうまくいきません。ここで私が間違っていることに誰かが集中してください。

public static boolean uploadMultipartData(String urlString, HashMap<String, Object> dataMap){
    HttpURLConnection urlConnection = null;
    DataOutputStream outStream = null;
    DataInputStream inStream = null;

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "---------------------------265001916915724";



    if (dataMap != null){
        try {
            StringBuffer data = new StringBuffer();
            URL url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setUseCaches(false);

            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Connection", "Keep-Alive");
            urlConnection.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);

            outStream = new DataOutputStream(urlConnection.getOutputStream());

            for (Entry<String, Object> entry : dataMap.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();

                outStream.writeBytes(twoHyphens + boundary + lineEnd);
                outStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\""
                                + lineEnd);
                outStream.writeBytes(value.toString());
                outStream.writeBytes(lineEnd);
            }
            outStream.writeBytes(twoHyphens + boundary + twoHyphens);
            outStream.flush();
        } 
        catch (MalformedURLException e) {
            Log.e("DEBUG", "[MalformedURLException while sending data]");
        } 
        catch (IOException e) {
            Log.e("DEBUG", "[IOException while sending data]");
        }
        finally{
            try {                   
                outStream.close();
                urlConnection.disconnect();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
4

1 に答える 1