RESTAPIを使用してファイルをアップロードするIntentServiceを構築しようとしています。
メインのアクティビティからIntentServiceを開始でき、IntentServiceがメッセージをアクティビティに返すことができるので、IntentServiceが機能していることがわかります。
私の問題は、このコードを実行するときです。
@Override
protected void onHandleIntent(Intent intent) {
// do the uploading stuff
try {
URL url = new URL(this.url + fileName);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
conn.setDoOutput(true);
BufferedInputStream buffInputStream = new BufferedInputStream(new FileInputStream(filePath));
BufferedOutputStream buffOutputStream = new BufferedOutputStream(conn.getOutputStream());
HttpUtils.copyBufferStream(buffInputStream, buffOutputStream);
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
BufferedInputStream responseBufferedInput = new BufferedInputStream(conn.getInputStream());
byte[] body = HttpUtils.readStream(responseBufferedInput);
result = HttpUtils.convertBytesToString(body);
Log.e("Result:", result);
}else{
Log.e("conn.getResponseCode()", Integer.toString(conn.getResponseCode()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
IntentService onHandleIntentメソッドでは、常に404(conn.getResponseCode()をチェックしていない場合はFileNotFoundException)が発生します。
メインのアクティビティでまったく同じコードを呼び出すと、ファイルがアップロードされ、正常に完了します。
なぜこれが起こっているのか分かりませんか?何か案は?