0

I am uploading my json file to php server but it gives me file not found. I have created it dynamically and it shows me in device. I have given same path. I am referring this https://coderwall.com/p/zjuwbg.

Code

       final String uploadFilePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        final String uploadFileName = "MyJson.json";
    FileObj = new File(uploadFilePath+"/"+uploadFileName);
                        System.out.println("file object : "+FileObj);
                        System.out.println("My file name: "+uploadFilePath+""+uploadFileName);
                        //uploadFile(uploadFilePath + "" + uploadFileName,userid);
                        StringBuffer responseBody=new StringBuffer();
                        Log.i("", "Ready to upload file...");
                        HttpClient client=new DefaultHttpClient();
                        client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
                        Log.i("", "Set remote URL...");
                        HttpPost post=new HttpPost("http://sripadamarketing.com/awarenessu/Data/");
                        File file = new File(uploadFilePath+"/"+uploadFileName);
                        MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                        ContentBody cbFile = new FileBody(file, "application/json");         
                        Log.i("emotinal awareness", "Adding file(s)...");

                        try {
                            entity.addPart("param", new StringBody("sync"));
                            entity.addPart("u", new StringBody(userid));
                            entity.addPart("jsonf", cbFile);
                            //entity.addPart("jsonf", new FileBody((FileObj), "application/json"));
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        //entity.addPart("uploadedfile2", new FileBody((FileObj), "application/zip"));
                        Log.i("emotinal awareness", "Set entity...");
                        post.setEntity(entity);
                        BufferedReader bs=null;
                        try
                        {
                         Log.i("emotinal awareness", "Upload...");
                          HttpEntity hEntity=client.execute(post).getEntity();
                          bs=new BufferedReader(new InputStreamReader(hEntity.getContent()));
                          Log.i("emotinal awareness", "Response length - "+hEntity.getContentLength());
                          String s="";
                          while(s!=null)
                          {
                            responseBody.append(s);
                            s=bs.readLine();
                            Log.i("emotinal awareness", "Response body - "+s);
                          }
                          bs.close();
                        }
                        catch(IOException ioe)
                        {
                         Log.i("emotinal awareness", "Error on getting response from Server, "+ioe.toString());
                          ioe.printStackTrace();
                          responseBody.append("...");
                        }

1 Answer 1

1

Try one of these libraries they are very good for such kind of tasks

https://github.com/loopj/android-async-http

Or

https://github.com/koush/ion

Sign up to request clarification or add additional context in comments.

2 Comments

You might want to be a bit careful; if those links ever go down, this answer will end up looking rather silly.
thank you very much for the advise. It will be better if you concentrate on the question he asked instead of posted answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.