1

Hello friend i m try lot of method to this cart append with 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}]";

this is one cart, append with this key mutirecords=, and send multiple images also, if i hit the url it give response but image not save in DB. how can send this cart with post method

  //
    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);

    }
Ram
  • 1,908
  • 3
  • 20
  • 30
  • 1
    you can only pass 2000 bytes in usr (GET) method. Rather use POST method to send additional data – Biraj Zalavadia Sep 11 '13 at 10:33
  • @BirajZalavadia so how can i send this with post method – Ram Sep 11 '13 at 10:34
  • I think this is a JSON object that you want to send – Biraj Zalavadia Sep 11 '13 at 10:35
  • You may have problem with special characters take a look at [this](http://stackoverflow.com/a/10000095/1289716) – MAC Sep 11 '13 at 10:35
  • your "imagedata" is a java object id. I guess you tried `Bitmap.toString()`... this will not deliver the image data you might want to deliver... – WarrenFaith Sep 11 '13 at 10:35
  • @BirajZalavadia : source for this 2000 value ? – njzk2 Sep 11 '13 at 10:36
  • It is a get method standard and another thing is also you cant send MIME data through GET method. You need to use Multipart entity to send images. – Biraj Zalavadia Sep 11 '13 at 10:38
  • @WarrenFaith so sir , what shall i do – Ram Sep 11 '13 at 10:38
  • @BirajZalavadia i know the Multipart entity but how to attached imagedata – Ram Sep 11 '13 at 10:40
  • You could try to encode it with [base64](http://stackoverflow.com/questions/4830711/how-to-convert-a-image-into-base64-string) and you should use JSON as request body with POST and not GET. – WarrenFaith Sep 11 '13 at 10:42
  • @BirajZalavadia : the rfc does not define any limit (although apache default max value is 8190), so do you have a specific source for quoting 2000 ? – njzk2 Sep 11 '13 at 10:42
  • @WarrenFaith if i using the post method sir , but how to attach mutirecords=[{imageid:1,size_id:8X11,cat_id:card,no_of_prints:1,addressid:26,user_id:91,device:android,imagedata:[B@41d46e98}] this – Ram Sep 11 '13 at 10:55
  • @Rishi than you are doing it wrong. – WarrenFaith Sep 11 '13 at 10:56
  • @WarrenFaith sir tel me the right way sir – Ram Sep 11 '13 at 10:57
  • First of all make sure you can use base64 correctly. Then move JSON and POST. If you have trouble, create a question, show the code you use and what your output is and what you expect it to be. Invest time into the question and you will see that people will help you. No code and such a generic question like above: no or only bad answers. So basically everything that @njzk2 wrote in his answer. – WarrenFaith Sep 11 '13 at 11:11
  • @WarrenFaith sir this my code – Ram Sep 11 '13 at 11:30
  • I stop here because you did not read completely what I wrote: `[...] create a question [...]`. Don't reuse that question you already posted. Beside the fact: You should format your code better (use ctrl+shift+f in eclipse, for example) – WarrenFaith Sep 11 '13 at 11:50
  • @WarrenFaith SIR HER IS MY CLASS http://pastie.org/private/3wgcytlawgopyzyncbuvga AND USING LIBRARY httpclient-4.1.jar ,httpcore-4.1.jar , httpmime-4.1.jar in proper Oder – Ram Sep 11 '13 at 12:48
  • I am about to quote myself: I stop here. You just don't read or you don't understand what I am trying to tell you. So I can't help. – WarrenFaith Sep 11 '13 at 12:50
  • @WarrenFaith lots of thanks finally did sir , thank you for valuable help ..... – Ram Sep 11 '13 at 14:53
  • What should I do with a comment like this?! Are you actually saying something?! – WarrenFaith Sep 13 '13 at 13:18
  • I really can't believe this... Email-ID? NO! – WarrenFaith Sep 13 '13 at 13:30

2 Answers2

1

finally got the solution public byte[] data;

    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
        }
Ram
  • 1,908
  • 3
  • 20
  • 30
0

[B@41d46e98 is not your actual image. It is the toString() of a byte[], which is why your image is not saved in db.

However, it is very unlikely that you can pass the complete byte[] in the url. Apache default max length is 8190, which make a small image.

You need to POST it. The easiest way would be using inline base64 for you image, provided it is not too big (which is an issue if you don't use a stream parser), otherwise using multipart or separated requests for the image and the metadata.

njzk2
  • 37,030
  • 6
  • 63
  • 102
  • i know multipart way but problem is that sir add the array mutirecords=[{imageid:1,size_id:8X11,cat_id:card,no_of_prints:1,addressid:26,user_id:91,device:android,imagedata:[B@41d46e98}] – Ram Sep 11 '13 at 10:53
  • the first issue you have is with the encoding of your byte[] – njzk2 Sep 11 '13 at 11:02
  • sir here is my code sir plz check http://pastie.org/private/3wgcytlawgopyzyncbuvga – Ram Sep 11 '13 at 13:06