[英]How to display captcha in ImageView in Android.?
我在Google Play上有一個PNR查詢應用程序。 運行得很好。 但是最近Indian Railwys在他們的PNR查詢部分添加了驗證碼,因此,我無法將正確的數據傳遞給服務器以獲取正確的響應。 如何以imageview的形式在我的應用程序中添加此驗證碼,並要求用戶也輸入驗證碼詳細信息,以便我可以發送適當的數據並獲得適當的響應。
如果您檢查html代碼,它實際上是非常糟糕的驗證碼。 驗證碼的背景是: http : //www.indianrail.gov.in/1.jpg這些數字實際上在輸入標簽中:
<input name="lccp_cap_val" value="14167" id="txtCaptcha" type="hidden">
他們正在通過JavaScript使用隱藏輸入標簽中的數字,並將其放在具有“驗證碼”背景的范圍內。
因此,基本上,您的流程是:
閱讀他們的html
從輸入字段中獲取“驗證碼”(大聲笑,雖然有趣的驗證碼)值
當用戶將數據放入您的PNR字段並按獲取狀態時
發布表格字段,將PNR設置為適當的值,將驗證碼設置為適當的值
解析響應
哦,是的,還有一件事。 您可以在隱藏輸入和“驗證碼”輸入中輸入任何值,只要它們相同即可。 他們沒有通過會話或其他任何方式對其進行檢查。
編輯(提交表單的代碼示例):為了簡化發布表單,我建議從Apache獲得HttpClient組件: http : //hc.apache.org/downloads.cgi假設您下載了HttpClient 4.3.1。 在您的項目中包括客戶端庫,核心庫和mime庫(復制到libs文件夾,右鍵單擊項目,屬性,Java Build Path,Libraries,Add Jars->添加那些3.)。
代碼示例為:
private static final String FORM_TARGET = "http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi.cgi";
private static final String INPUT_PNR = "lccp_pnrno1";
private static final String INPUT_CAPTCHA = "lccp_capinp_val";
private static final String INPUT_CAPTCHA_HIDDEN = "lccp_cap_val";
private void getHtml(String userPnr) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(INPUT_PNR, userPnr); // users PNR code
builder.addTextBody(INPUT_CAPTCHA, "123456");
builder.addTextBody("submit", "Get Status");
builder.addTextBody(INPUT_CAPTCHA_HIDDEN, "123456"); // values don't
// matter as
// long as they
// are the same
HttpEntity entity = builder.build();
HttpPost httpPost = new HttpPost(FORM_TARGET);
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
String htmlString = "";
try {
response = client.execute(httpPost);
htmlString = convertStreamToString(response.getEntity().getContent());
// now you can parse this string to get data you require.
} catch (Exception letsIgnoreItForNow) {
}
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException ignoredOnceMore) {
} finally {
try {
is.close();
} catch (IOException manyIgnoredExceptions) {
}
}
return sb.toString();
}
另外,請注意,我沒有將其包裝在異步調用中,因此您必須這樣做。
來自網絡的圖像可以通過高效的圖像加載API(例如Picasso / volley)顯示在android中,也可以通過異步任務僅顯示圖像。
考慮到以上所有內容都是基本的構建邏輯,因此如果用戶重置或刷新驗證碼,則需要該驗證碼的圖像URL,如果用戶應通過請求新請求實現的網絡調用重新加載新圖像,則必須獲得REST api訪問權限印度鐵路,並檢查是否有任何可用的圖像uri(可能為base64格式)
如果REST API不可用,您可能會考慮使用此代碼構建自己的服務器
更新:您不需要執行這種復雜的技巧,只需實現Drago的答案即可!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.