将图像和其他参数上传到服务器

时间:2016-08-03 13:33:37

标签: android android-volley

我是使用排球更新图像和一些文本到服务器,但图像没有上传。我搜索了它,发现图像需要使用multipart发送,而volley不支持multipart。有没有办法使用凌空上传图像和文本?



std::string




1 个答案:

答案 0 :(得分:2)

尝试这种方式:



class ImageUploadTask extends AsyncTask<String, Void, String> {




        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            try {

                String url = URLUtils.URL;

                HttpClient httpClient = new DefaultHttpClient();
                //HttpContext localContext = new BasicHttpContext();
                HttpPost httpPost = new HttpPost(url);
                MultipartEntity entity = new MultipartEntity();

                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                byte[] data = bos.toByteArray();

                entity.addPart("auth", new StringBody("abc167"));
                entity.addPart("action", new StringBody("editprofile"));
                entity.addPart("first_name", new StringBody(fname));
                entity.addPart("last_name", new StringBody(lname));
                entity.addPart("address", new StringBody(address_str));
                entity.addPart("zipcode", new StringBody(zip));
                entity.addPart("phoneno", new StringBody(phone));
                entity.addPart("device_token_id", new StringBody("1234567890"));
                entity.addPart("device_type", new StringBody("android"));
                entity.addPart("lat", new StringBody(lat_str));
                entity.addPart("long", new StringBody(lng_str));
                entity.addPart("userid", new StringBody("121"));
                entity.addPart("photo", new ByteArrayBody(data, "myimage.jpg"));

                httpPost.setEntity(entity);
                HttpResponse response = httpClient.execute(httpPost);
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent(), "UTF-8"));
                String sResponse;
                 s = new StringBuilder();

                while ((sResponse = reader.readLine()) != null) {
                    s = s.append(sResponse);
                }
                System.out.println("Response: " + s);

            } catch (Exception e) {

                Log.e(e.getClass().getName(), e.getMessage(), e);

            }
            return s.toString();
        }

        @Override
        protected void onPostExecute(String sResponse) {
            try {

                if (sResponse != null) {
                    Toast.makeText(getApplicationContext(),
                            sResponse + " Photo uploaded successfully",
                            Toast.LENGTH_SHORT).show();

                }

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getMessage(),
                        Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
            }

        }
    }
&#13;
&#13;
&#13;