简体   繁体   中英

I am receiving a pdf format string from a web service. I want to generate a pdf for it on button click

This is the code for calling the service.This method is getting called from the main code.

String pdf ;

public String getTestReportPDF(JSONObject sendData) {

    url = "http:/*************/";
    request = new HttpPost(url);
    request.setHeader("Content-type", "application/json");
    request.setHeader("Accept", "application/json");
    request.setHeader("Cookie", hoja);
    String cookieData = "";

    for (int i = 0; i < cookies.length; i++) {
        cookieData += cookies[i].getValue() + ";";
    }

    request.addHeader("Cookie", cookieData);

    try {
        request.setEntity(new StringEntity(sendData.toString(), "UTF-8"));
        response = client.execute(request);


        reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        System.out.println(reader);
        sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        pdf = sb.toString();







    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return pdf;
}

The string i am receiving looks like garbage text. But it is to be stored to get a pdf This is my code for storing it:

            File sdCard = Environment.getExternalStorageDirectory();
                File reportFile = null;
                File dir = new File(sdCard.getAbsolutePath() + "/HIS Patient/");
                if (!dir.exists()) {
                    dir.mkdirs();
                }


                pdf = service.getTestReportPDF(sendData);
                System.out.println(pdf);
                byte[] b = pdf.getBytes("UTF-8");
                //byte[] pdfAsBytes = UTF-8.decode(pdf, 0);
                System.out.println(b);


                reportFile = new File(dir.getAbsolutePath(),
                        "report.pdf");
                //result = service.getTestReportPDF(reportData);
                String temp = null;
                temp = new String(b);
                Log.v("View & result==null",
                        reportFile.getAbsolutePath());
                Log.v("Content of PDF", temp);
                OutputStream out;
                try {
                    out = new FileOutputStream(reportFile);
                    out.write(b);
                    out.flush();
                    out.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }





                File file = null;
                File fileReport = new File(dir.getAbsolutePath(),"report.pdf");
                if (!fileReport.exists()) {
                    file = new File(dir.getAbsolutePath(), "reportName" + ".pdf");
                } else {
                    file = new File(dir.getAbsolutePath(), "report.pdf");
                }
                PackageManager packageManager = getPackageManager();
                Log.e("View path=>", file.getAbsolutePath());
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setType("application/pdf");

                @SuppressWarnings("rawtypes")
                List list = packageManager.queryIntentActivities(intent,
                        PackageManager.MATCH_DEFAULT_ONLY);

                if (list.size() > 0 && file.isFile()) {
                    Log.v("post", "execute");
                    Intent i = new Intent();
                    i.setAction(Intent.ACTION_VIEW);
                    Uri uri = Uri.fromFile(file);
                    i.setDataAndType(uri, "application/pdf");
                    startActivityForResult(i, 1792);

                } else if (!file.isFile()) {
                    Log.v("ERROR!!!!", "OOPS2");
                } else if (list.size() <= 0) {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(ReportStatus.this);
                    dialog.setTitle("PDF Reader not found");
                    dialog.setMessage("A PDF Reader was not found on your device. The Report is saved at "
                            + file.getAbsolutePath());
                    dialog.setCancelable(false);
                    dialog.setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    dialog.dismiss();
                                }
                            });
                    dialog.show();
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM