2017-05-23 1 views
0

私はいくつかの以前の質問をしましたが、適切な解決策を得られませんでした。私はアプリケーションを作成しており、ファイルマネージャからPDFファイルを選択して送信します。PDFファイルをAndroidにサーバーにアップロードするにはどうすればいいですか?

ありがとうございました。

+0

何を試してみましたか、具体的な問題はありますか? – CommonsWare

+0

これまでに試したことをアップロードしてください。 –

+1

diffecenceはありません。ファイルの種類は異なります。すべてのファイルは、PDFまたはMP3またはZIPファイルとは関係なく、同じ方法で送信できます。変わりはない。だからあなたは 'サーバにファイルをアップロードする方法'を知りたいのですか? –

答えて

2

ギャラリーからPDFファイルを選択する必要がある場合のみ、このコード行を変更する必要があります。 intent.setType( "application/pdf") これはギャラリーからPDFファイルのみを検索します。

Intent intent = new Intent(); 
    intent.setType("application/pdf"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PDF); 
0

Okhttp libraryをこのように使用するのが最も簡単な方法です。しかし、これに応じてサーバー(APIまたはPHP)のコードを変更する必要があります。

RequestBody requestBody = new MultipartBody.Builder() 
      .setType(MultipartBody.FORM) 
      .addFormDataPart("variable", fileName1, 
        RequestBody.create(MediaType.parse(fileType1), file)) 
      .addFormDataPart("key", "") 


      .build(); 
    Request request = new Request.Builder().url("server url goes here").post(requestBody).build(); 
    okhttp3.Call call = client.newCall(request); 
    call.enqueue(new Callback() { 

     @Override 
     public void onFailure(Call call, IOException e) { 
      System.out.println("Registration Error" + e.getMessage()); 
     } 

     @Override 
     public void onResponse(Call call, okhttp3.Response response) throws IOException { 

      try { 
       String resp = response.body().string(); 
       Log.v("Docs", resp); 

      } catch (IOException e) { 
       System.out.println("Exception caught" + e.getMessage()); 
      } 
     } 

    }); 
関連する問題