Web サービスからいくつかのファイルをダウンロードする必要がある Android アプリケーションを開発しています。
ゴール
Web サービスから PDF ファイルをダウンロードして、電話で表示したい。
問題
私はWebサービスを扱っている初心者で、ファイルを取得して開く方法がはっきりしていません。
これまでの私のコードは次のとおりです。
public void downloadFile(String username, String password, String filename) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("filename", filename));
String output = httpPost(URL_DESCARGAS, nameValuePairs);
}
private String httpPost(String url, List<NameValuePair> nameValuePairs) {
/* Create new HttpClient and Post Header */
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
/* Get output */
return inputStreamToString(response.getEntity().getContent()).toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
レスポンスの文字列を返さなくてもいいのですが、何をすればいいのかわかりません。では、Web サービスを返す PDF を表示するにはどうすればよいでしょうか。
別のアクティビティでは、同じことを実行する必要がありますが、Web サービスは .png を返し、それをイメージビューなどで表示したいと考えています。それは同じように行われますか?
どうもありがとうございました。長い間探しましたが、何も見つかりませんでした。
編集
私はそのURLを見つけました: http://www.androidsnippets.com/download-an-http-file-to-sdcard-with-progress-notification と私はそれが言うことを使用しますが、ファイルは作成されません(私は' SDカードルートに表示されません)。
これが私の変更されたコードです:
public void downloadFile(String username, String password, String filename) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("filename", filename));
HttpResponse response = httpPost(URL_DESCARGAS, nameValuePairs);
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
InputStream inputStream = entity.getContent();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot, filename);
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
}
//close the output stream when done
fileOutput.close();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
手伝って頂けますか?そのファイルを作成して、今すぐ電話で表示する必要があります。