1

你好朋友我尝试了很多方法到这个购物车附加 url

  url = "http://xxxxxxxxxxxx/index.php?option=com_storephotos&view=storephotos&storemp=storemp&imageid=73&task=storemp&device=android&mutirecords=[{imageid:1,size_id:8X11,cat_id:card,no_of_prints:1,addressid:26,user_id:91,device:android,imagedata:[B@41d46e98}]";

这是一个购物车,附加此键 mutirecords=,并发送多个图像,如果我点击 url 它会给出响应但图像不会保存在数据库中。如何使用 post 方法发送此购物车

  //
    protected Void doInBackground(Void... params) {
        try {

            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            if (data != null) {
                System.out.println("aaaaa");
                entity.addPart("userfile:", new ByteArrayBody(data,
                        "pic.jpg"));

            } else {
                System.out.println(",,,,,,,,,,,,,,,,,,,,,,");
                data = "nopick...........".getBytes();

    //                  entity.addPart("imagedata:", new  


   ByteArrayBody(data,

//                          "pic.jpg"));
            }



            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("imageid:",
                    "1"));
            // .......
            nameValuePairs.add(new BasicNameValuePair("size_id:", "8X11"));
            nameValuePairs.add(new BasicNameValuePair("cat_id:", "card"));

            nameValuePairs
                    .add(new BasicNameValuePair("no_of_prints:", "1"));
            nameValuePairs.add(new BasicNameValuePair("addressid:", "26"));

            nameValuePairs.add(new BasicNameValuePair("user_id:", "91"));
            String as = "mmmlocation" + "},";
            System.out.println("ass  " + as);
            nameValuePairs
                    .add(new BasicNameValuePair("device:", "android"));


            for (int i = 0; i < nameValuePairs.size(); i++) {

                try {

                    Log.d("sss", "vffff" + i);
                    System.out.println(nameValuePairs.get(i).getName()
                            + " nnhhh "
                            + new StringBody(nameValuePairs.get(i)
                                    .getValue()));
                    Log.d("sss", "vffff" + i);
                    entity.addPart(
                            nameValuePairs.get(i).getName(),
                            new StringBody(nameValuePairs.get(i).getValue()));

                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    Log.d("respons", "image respons " + e);
                    e.printStackTrace();
                }

            }

            HttpClient httpclient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httppost = new HttpPost(
                    "http://xxxxxxxxxxxxxx/index.php?option=com_storephotos&view=storephotos&storemp=storemp&imageid=73&task=storemp&device=android&mutirecords=");
            Log.d("entity", "entity " + entity);
            httppost.setEntity(entity);
            HttpResponse response = httpclient.execute(httppost,
                    localContext);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));

            String sResponse = reader.readLine();
            Log.d("re", "res" + sResponse);

            // bitmapOrg.recycle();
            //

        } catch (Exception e) {
            // // TODO: handle exception
            if (dialog.isShowing())
                dialog.dismiss();

            Log.d("aaaaaaaaa", "aaaaaaaaa " + e);

        }
        return null;

        // (null);

    }
4

2 回答 2

1

终于得到解决方案 public byte[] 数据;

    HttpURLConnection connection;
        OutputStreamWriter request = null;

        URL url = null;
        String response = null;
        String image_str = Base64.encodeBytes(data);
        byte[] bytes = null;
        String newww = null;
        try {
            // bytes = new byte[1024];
            newww = URLEncoder.encode(image_str, "UTF-8");
            bytes = image_str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         String parameters =
     "option=com_storephotos&view=storephotos&storemp=storemp&imageid=73&task=storemp&device=android&mutirecords=[{imageid:1,size_id:8X11,cat_id:card,no_of_prints:1,addressid:26,user_id:91,device:android,imagedata:"
        // + newww + "}]";


        try {
            url = new URL(
                    "http://xxxxxxxxxxxxxx/index.php");
            Log.d("url", "url " + url);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");

            request = new OutputStreamWriter(connection.getOutputStream());
            request.write(parameters);
            request.flush();
            request.close();
            String line = "";
            InputStreamReader isr = new InputStreamReader(
                    connection.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            response = sb.toString();

            Log.d("response", "response " + response);

            isr.close();
            reader.close();

        } catch (IOException e) {
            // Error
        }
于 2013-09-12T04:35:21.183 回答
0

[B@41d46e98不是你的实际形象。它是toString()a 的byte[],这就是为什么您的图像没有保存在 db 中的原因。

但是,您不太可能在 url 中传递完整的 byte[]。Apache 默认最大长度为 8190,这会生成一个小图像。

您需要发布它。最简单的方法是为您的图像使用内联 base64,前提是它不是太大(如果您不使用流解析器,这是一个问题),否则对图像和元数据使用多部分或分离的请求。

于 2013-09-11T10:38:18.977 回答