I would like the user to enter the product code (in activity_main.xml
) and click the button.
The screen changes ( view_pdf.xml
), it would download the PDF ( AnsyTask
) and it would open in PDFView
by pdfView.fromStream
.
I use Android Studio and Spring Boot.
MainActivity
public class MainActivity extends AppCompatActivity {
private ProgressDialog load;
private TextView codigoModel;
private Button bt_busca;
private PDFView pdfView;
private ProgressDialog loadPdf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
codigoModel = (TextView) findViewById(R.id.codigoModelo);
bt_busca = (Button) findViewById(R.id.bt_buscar);
pdfView = (PDFView) findViewById(R.id.pdfView);
bt_busca.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setContentView(R.layout.view_pdf);
chamarAsyncTask(codigoModel.getText().toString());
}
});
}
private void chamarAsyncTask(String codigoModel) {
DownloadPdfView downloadPdfView = new DownloadPdfView();
Log.i("AsyncTask", "AsyncTask senado chamado Thread: " + Thread.currentThread().getName());
downloadPdfView.execute(codigoModel);
}
private class DownloadPdfView extends AsyncTask<String, Void, InputStream> {
@Override
protected void onPreExecute() {
Log.i("AsyncTask", "Exibindo ProgressDialog na tela Thread: " + Thread.currentThread().getName());
loadPdf = ProgressDialog.show(MainActivity.this, "Por Favor Aguarde ...",
"Baixando Desenho ...");
}
@Override
protected InputStream doInBackground(String... params) {
InputStream inputStream;
AcessoWebService url = new AcessoWebService();
String codigo = params[0];
Log.i("AsyncTask", "Baixando o PDF Thread: " + Thread.currentThread().getName());
AcessoWebService acessoWebService = new AcessoWebService();
inputStream = acessoWebService.getApiInputStream("http://192.168.100.100:8080/produto/download/" + codigo);
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdfView.fromStream(inputStream).load();
}
}
}
Web Service Access
public class AcessoWebService {
public InputStream getApiInputStream(String url) {
int codigoResposta;
HttpURLConnection conexao;
InputStream inputStream = null;
try {
URL apiEnd = new URL(url);
conexao = (HttpURLConnection) apiEnd.openConnection();
conexao.setRequestMethod("GET");
conexao.setReadTimeout(15000);
conexao.setConnectTimeout(15000);
conexao.connect();
codigoResposta = conexao.getResponseCode();
if (codigoResposta < HttpURLConnection.HTTP_BAD_REQUEST) {
inputStream = new BufferedInputStream(conexao.getInputStream());
} else {
inputStream = conexao.getErrorStream();
}
inputStream.close();
conexao.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
}
Dependencies
//Add Library
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
compile 'org.apache.commons:commons-collections4:4.1'
Exception
E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.mobile, PID: 16834
java.lang.NullPointerException: Attempt to invoke virtual method 'com.github.barteksc.pdfviewer.PDFView$Configurator com.github.barteksc.pdfviewer.PDFView.fromStream(java.io.InputStream)' on a null object reference
at br.com.mobile.MainActivity$DownloadPdfView.onPostExecute(MainActivity.java:100)
at br.com.mobile.MainActivity$DownloadPdfView.onPostExecute(MainActivity.java:54)