1

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)が発生します。

メインのアクティビティでまったく同じコードを呼び出すと、ファイルがアップロードされ、正常に完了します。

なぜこれが起こっているのか分かりませんか?何か案は?

4

1 に答える 1

5

開こうとしているURLを印刷してみてください。おそらく、何か問題があることがわかります...たぶん、スラッシュが欠落しているだけです;-)

404は、見つかりませんのHTTPエラーです。URLがそのサーバーで無効であることを意味します。

于 2013-02-22T11:17:21.533 に答える