如何使用Android中的MultipartEntity发送多个字符串变量



我正在尝试使用MultipartEntity发送图像和纬度和经度值。纬度和经度的值存储在一个名为plat和plong的单独变量中。我可以将图像发送到服务器,但不能同时发送纬度和经度。在服务器端,只有Latitude最终插入数据库。Bellow是我用来将文件和字符串发送到服务器的代码。有人能告诉我哪里做错了吗?

 private class UploadTask extends AsyncTask<Bitmap, Void, Void> {
    protected Void doInBackground(Bitmap... bitmaps) {
        if (bitmaps[0] == null)
            return null;
        setProgress(0);
        Bitmap bitmap = bitmaps[0];
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); // convert
                                                                    // Bitmap
                                                                    // to
                                                                    // ByteArrayOutputStream
        InputStream in = new ByteArrayInputStream(stream.toByteArray()); // convert
                                                                            // ByteArrayOutputStream
                                                                            // to
                                                                            // ByteArrayInputStream
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            HttpPost httppost = new HttpPost(
                    "http://www.example.com/index.php"); // server
            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("myFile",
                    System.currentTimeMillis() + ".jpg", in);
            httppost.setEntity(reqEntity);
            reqEntity.addPart("long",String.valueOf(plong));
            reqEntity.addPart("lat",String.valueOf(plat));

            Log.e(TAG, "request " + httppost.getRequestLine());
            HttpResponse response = null;
            try {
                response = httpclient.execute(httppost);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (response != null)
                    Log.i(TAG, "response "
                            + response.getStatusLine().toString());
            } finally {
            }
        } finally {
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

下面是MultipartEntity类中的addPart()方法。

ByteArrayOutputStream out = new ByteArrayOutputStream();
public void addPart(final String key, final String value) {
    writeFirstBoundaryIfNeeds();
    try {
        out.write(("Content-Disposition: form-data; name="" +key+""rn").getBytes());
        out.write("Content-Type: text/plain; charset=UTF-8rn".getBytes());
        out.write("Content-Transfer-Encoding: 8bitrnrn".getBytes());
        out.write(value.getBytes());
        out.write(("rn--" + boundary + "rn").getBytes());
    } catch (final IOException e) {
    }
}

尝试使用这个,

reqEntity.addPart("long",new StringBody(plong));
reqEntity.addPart("lat",new StringBody(plat));
// or plong.toString() and plat.toString() depending on your plong and plat datatype
//and then add to httppost
httppost.setEntity(reqEntity);

发送纬度和经度。

然而,你可以查看这个链接,这里有很好的例子,对理解非常有帮助

在添加请求部分后设置实体,并将new StringBody()添加到String.valueOf():

这样做,可以帮助你吗:-

 MultipartEntity reqEntity = new MultipartEntity();
 reqEntity.addPart("myFile",
 System.currentTimeMillis() + ".jpg", in);
 reqEntity.addPart("long",new StringBody(String.valueOf(plong)));
 reqEntity.addPart("lat",new StringBody(String.valueOf(plat)));
 httppost.setEntity(reqEntity);

yes@Harsh是对的。。。您正在设置实体。。。。在将部件数据添加到multipartentity之前。。。。

更改代码。。。

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("myFile",
                System.currentTimeMillis() + ".jpg", in);
        httppost.setEntity(reqEntity);
        reqEntity.addPart("long",String.valueOf(plong));
        reqEntity.addPart("lat",String.valueOf(plat));

对此。。。

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("myFile",
                System.currentTimeMillis() + ".jpg", in);
        //httppost.setEntity(reqEntity);//remove this line and place after setting all part values
        reqEntity.addPart("long",new StringBody(plong));
        reqEntity.addPart("lat",new StringBody(plat));
        httppost.setEntity(reqEntity); //place it here....

相关内容

  • 没有找到相关文章

最新更新