-1

i want to use the return of a request http "result"(method get data) in other method to create file name with result included in the file name. thanks for your help

first method
 public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://192.168.x.x/x/id1.php");

                // Depends on your web service


                InputStream inputStream = null;
               String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");

                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

            @Override
            protected void onPostExecute(String  result){
                 myJSON =result;

            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }

second method

  private static File getOutputMediaFile(int type) {

            // External sdcard location
            File mediaStorageDir = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    Config.IMAGE_DIRECTORY_NAME);

            // Create the storage directory if it does not exist
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d(TAG, "Oops! Failed create "
                            + Config.IMAGE_DIRECTORY_NAME + " directory");
                    return null;
                }
            }

            // Create a media file name  
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.getDefault()).format(new Date());
            File mediaFile
    //i want to use it here like "IMG_VG"+result+...
            if (type == MEDIA_TYPE_IMAGE) {

                mediaFile = new File(mediaStorageDir.getPath() + File.separator
                        + "IMG_VG" + timeStamp + ".jpg");
            } else if (type == MEDIA_TYPE_VIDEO) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator
                        + "VID_VG" + timeStamp + ".mp4");
            } else {
                return null;
            }

            return mediaFile;
        }
Anil
  • 1,087
  • 1
  • 11
  • 24
develop1
  • 11
  • 5
  • 1
    Possible duplicate of [Return a value from AsyncTask in Android](http://stackoverflow.com/questions/9458258/return-a-value-from-asynctask-in-android) – Tim May 30 '16 at 09:52
  • how I'll make that because result is related to the First method??Thanks – develop1 May 30 '16 at 09:57

1 Answers1

0

Why not using the second method within the AsyncTask? Instead of returning the downloaded data let it return a File that is created from or with the data in the 'doInBackground' method. Then return the File to the Activity in the 'onPostExecute'.

 public void getData(){
    class GetDataJSON extends AsyncTask<String, Void, File> {

        @Override
        protected File doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://192.168.x.x/x/id1.php");

            // Depends on your web service


            InputStream inputStream = null;
            File outputFile = getOutputMediaFile(0);
           String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                //TODO check if result is 200 OK or you'll write the error in the file.

                inputStream = entity.getContent();

                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");

                }
                result = sb.toString();

                FileWriter writer = new FileWriter(outputFile);
                writer.write(result);
                writer.close();
                return outputFile;
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return outputFile;
        }

        @Override
        protected void onPostExecute(File  result){
             myFile =result;

        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}
GPuschka
  • 522
  • 3
  • 9
  • the code is very complicated and i took it from a tutorial i tried but i failed:( can u give me please an other solution! Thanks – develop1 May 30 '16 at 10:26
  • Actually im very near to resolve it normally the result of my php file it's like that [{"id_visite":"49"}] but i get in the name file just IMG_VG[{ why? – develop1 May 30 '16 at 11:18
  • Thats because you're not passing the anything from the result to the name. In the second method the File is constructed as 'mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_VG" + timeStamp + ".jpg")'. You could pass some part of the result instead of the timeStamp. – GPuschka May 30 '16 at 11:36
  • i did the pass myjson mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_VG" +myJSON+ timeStamp + ".jpg"); and i have that protected void onPostExecute(String result){ myJSON =result; – develop1 May 30 '16 at 11:40
  • Ah, now I see. Quotes are not allowed in a filename. Construct a name from the json instead of using the json as the name. – GPuschka May 30 '16 at 11:45
  • aaah i see i will serach how to get just the number from my json data thank u – develop1 May 30 '16 at 11:48