2016-06-30 5 views
-1

正しいコードを送信してGoogleクラウドプリントにPDFを含むマルチパートフォームを送信すると問題が発生しています(アンドロイドでinbuiltの意図を使用していない理由はAndroid Google CloudPrint URL APIを使用して印刷ジョブを送信する

私はそれを提出するためにVolleyを使用していますが、大きなファイルではそれほど素晴らしいとは読んでいないのですか?試行錯誤の多くの後

final RequestQueue queue =  Volley1.getInstance(this).getRequestQueue(); 
      String url3 = "https://www.google.com/cloudprint/submit"; 
      final VolleyMultipartRequest multipartRequest = new  VolleyMultipartRequest(Request.Method.POST, url3 ,new    Response.Listener<NetworkResponse>() { 

        @Override 
        public void onResponse(NetworkResponse response) { 
          String resultResponse = new    String(response.data); 
          Log.d("responseis",resultResponse); 
        } 
      }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
          error.printStackTrace(); 
        } 
      }){ 
        @Override 
        protected Map<String, String> getParams() { 

          Map<String, String> params = new HashMap<>(); 
          // the POST parameters: 
          params.put("redirect_uri", "xxxxx"); 
          params.put("printerid","xxxxxx"); 
          params.put("title", "result1.pdf"); 
          params.put("contentType", "application/pdf"); 
          params.put("ticket", "{\"print\":{},\"version\":\"1.0\"}"); 
          params.put("hl", "en"); 
          return params; 
        } 
        @Override 
        public Map getHeaders() throws AuthFailureError { 
          Map headers = new HashMap(); 
          headers.put("Authorization", "OAuth"+" "+res); 
          headers.put("Content-Type", "multipart/form-data; boundary=__1466595844361__"); 
          //headers.put("Content-Type","application/x-www-form-urlencoded"); 
          //Logger.debugE(Constants.accesstoken, headers.toString()); 
          return headers; 
        } 
        @Override 
        protected Map<String, DataPart> getByteData() { 
          Map<String, DataPart> params = new HashMap<>(); 
          //String yourFilePath =  Environment.getExternalStorageDirectory().getAbsolutePath()+ "/PDFs/result1.pdf"; 
          String yourFilePath=Environment.getExternalStorageDirectory().getAbsolutePath()+ "/PDFs/result1.pdf"; 
          File dir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS), "PDF_reader"); 

          File text = new File(dir + File.separator + "test_r.pdf"); 
          String input = text.getAbsolutePath(); 
          byte[] data = new byte[(int) input.length()]; 
          try { 
            //convert file into array of bytes 
            data = FileUtils.readFileToByteArray(text); 

          }catch(Exception e){ 
            e.printStackTrace(); 
          } 

          params.put("content", new DataPart("result1.pdf",data , "application/pdf")); 
          return params; 
        } 
      }; 
queue.add(multipartrequest); 
+0

申し訳ありませんが、この問題を示す「最小の例」にはなりません。関連するコードを数百行以内で見つけることはここにはありません。 – Eiko

答えて

0

私はOKHTTPライブラリを使用して終了し、私は正常にAPIを使用してGoogleクラウドプリントに印刷ジョブを送信することができました。 pdfファイルを含むマルチパートフォームを入手するのはちょっと手間がかかりましたが、正しい作業要求は以下のとおりです。明らかに、トークンとprinteridは他の要求で取り出され、手動で追加する必要があります。

public void run() throws Exception { 

//File directory 
File dir = Environment.getExternalStorageDirectory(); 
File file = new File(dir, "PDFs/result1.pdf"); 
if(file.canRead()){ 
    Log.d("file", "can read"); 
}else {Log.d("file","cant read");}; 


// Final request body 
RequestBody requestBody = new MultipartBody.Builder() 
     .setType(MultipartBody.FORM) 
     .addFormDataPart("content","result1.pdf",RequestBody.create(MediaType.parse("application/pdf"), file)) 
     .build(); 

//URL Parameters 
HttpUrl url = new HttpUrl.Builder() 
     .scheme("https") 
     .host("www.google.com") 
     .addPathSegments("cloudprint/submit") 
     .addQueryParameter("printerid", "82d70sde-694b-3385-f263-164cb04320c3") 
     .build(); 

Request request = new Request.Builder() 
     .header("Authorization", "OAuth ya29.Cl0jAxDuQni_Dskz6Y2r-wRaJVMxEw8fy5hPNfAm02pDLnZc9HX-RfHpmMoS0OL-Wv_SKxBtYIwRK9jVpJDwl7Qs-RFt02Qc5Yoid4w1kV8b4vBIpcleIQ8lBEto") 
     .url(url) 
     .post(requestBody) 
     .build(); 

client.newCall(request) 
     .enqueue(new Callback() { 
      @Override 
      public void onFailure(final Call call, IOException e) { 
       // Error 

       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         // For the example, you can show an error dialog or a toast 
         // on the main UI thread 
        } 
       }); 
      } 

      @Override 
      public void onResponse(Call call, final Response response) throws IOException { 
       String res = response.body().string(); 

       // Do something with the response 
      } 
     }); 
} 
関連する問題