我使用本教程将我的 android 应用程序与 Google Cloud Printer 集成。它工作正常,但我想在没有WebView的情况下这样做。
我想用两个步骤打印:
- 生成pdf文件
- 单击“打印”按钮并提交打印作业
我将我的打印机共享给任何有链接的人,并希望将我的应用程序中的所有 pdf 文件打印到我的共享打印机。
你能分享一些代码或教程吗?
对不起我的英语不好
我使用本教程将我的 android 应用程序与 Google Cloud Printer 集成。它工作正常,但我想在没有WebView的情况下这样做。
我想用两个步骤打印:
我将我的打印机共享给任何有链接的人,并希望将我的应用程序中的所有 pdf 文件打印到我的共享打印机。
你能分享一些代码或教程吗?
对不起我的英语不好
我找到了这个链接,它对我有帮助
添加:
@Override
protected Void doInBackground(Void... params) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
String user = "user@gmail.com";
String pass = "password";
String source = "Cloud%20Printing%20Test";
HttpGet authGet = new HttpGet(
"https://www.google.com/accounts/ClientLogin?accountType=HOSTED_OR_GOOGLE&Email="
+ user
+ "&Passwd="
+ pass
+ "&service=cloudprint&source=" + source);
HttpResponse httpResponse;
httpResponse = httpclient.execute(authGet);
String authResponse = EntityUtils
.toString(httpResponse.getEntity());
String authKey = authResponse.substring(authResponse
.indexOf("Auth=") + 5);
authKey = authKey.replace("\n", "");
MyLog.d(TAG, "Auth key: " + authKey);
HttpPost printPost = new HttpPost(
"https://www.google.com/cloudprint/submit?output=json");
printPost.setHeader("Authorization", "GoogleLogin auth=" + authKey);
printPost.setHeader("X-CloudPrint-Proxy", source);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("printerid", "ID"));
nameValuePairs.add(new BasicNameValuePair("title", "TEST"));
nameValuePairs.add(new BasicNameValuePair("capabilities", "{capabilities=[]}"));
nameValuePairs.add(new BasicNameValuePair("content", "123"));
nameValuePairs.add(new BasicNameValuePair("contentType", "text/plain"));
printPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse printResponse = httpclient.execute(printPost);
String lol = EntityUtils.toString(printResponse.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
但现在我只能打印文本。如果我找到了如何打印 pdf 的解决方案 - 我将在此处发布代码
添加2:
此代码发送 pdf 文件进行打印
File file = new File("file.pdf");
FileBody fb = new FileBody(file);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addTextBody("printerid", "ID");
builder.addTextBody("title", "TEST2");
builder.addTextBody("capabilities", "{capabilities=[]}");
builder.addTextBody("contentType", "application/pdf");
builder.addPart("content", fb);
printPost.setEntity(builder.build());