2015-09-18 70 views
0

我是新來的。我最近3天面臨這個問題。我們有Web應用程序和Android應用程序。如何在向客戶端發送請求後從webview獲得響應

在Web應用程序中,我們從客戶端接收請求(POST方法),並根據結果重定向到成功或失敗URL。

現在的Android應用程序的問題:使用Webview我發送同樣的請求到服務器。我的代碼正在執行,但我不知道幾件事情。

  1. 什麼是Android的成功與失敗URL?

  2. 如何從客戶端獲得響應並返回活動?

默認情況下,我給成功的網址是Google.com,失敗的網址是Gmail.com。如果我給我的Web應用程序成功和失敗的URL,它會在服務器端提供響應。

注:在這段代碼中,我不知道如何使用JavaScript代碼。我指的是別的地方。 PayUJavaScriptInterface,success()popup()方法不使用。如果我必須用來獲得迴應,然後告訴我如何使用。

這裏是我的代碼爲我的活動

public class PayMentGateWay extends Activity { 
private ArrayList<String> post_val = new ArrayList<String>(); 
private String post_Data=""; 
WebView webView ; 
final Activity activity = this; 
private String tag = "PayMentGateWay"; 
private String hash,hashSequence; 
String merchant_key="JBZaLc"; 
    String salt="GQs7yium"; 
    String action1 =""; 
    String base_url="https://test.payu.in"; 
    int error=0; 
    String hashString=""; 
    Map<String,String> params; 
    String txnid =""; 

    Handler mHandler = new Handler(); 


@SuppressLint("JavascriptInterface") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    final ProgressDialog progressDialog = new ProgressDialog(activity); 
    getWindow().requestFeature(Window.FEATURE_PROGRESS); 
    webView = new WebView(this); 
    setContentView(webView); 

    post_val.add("key"); 
    post_val.add("JBZaLc"); 
    post_val.add("txnid"); 
    post_val.add("5d59dae66618a14f5020"); 
    post_val.add("amount"); 
    post_val.add("100.00"); 
    post_val.add("productinfo"); 

    post_val.add("{paymentParts:[{name:abc,description:abcd,value:500,isRequired:true,settlementEvent:EmailConfirmation}], paymentIdentifiers:[{field:CompletionDate, value:25/06/2015}]}"); 
    post_val.add("firstname"); 
    post_val.add("pqrs"); 
    post_val.add("email"); 
    post_val.add("[email protected]"); 
    post_val.add("phone"); 
    post_val.add("xxxxxxxxxx"); 
    post_val.add("surl"); 
    post_val.add("https://www.google.com"); 
    post_val.add("furl"); 
    post_val.add("https://www.gmail.com"); 
    post_val.add("hash"); 
    post_val.add(""); 
    post_val.add("provider"); 
    post_val.add("payu_paisa"); 
    Log.d(tag, "post_val: "+post_val); 
    params= new HashMap<String,String>(); 
    for(int i = 0;i<post_val.size();){ 
     params.put(post_val.get(i), post_val.get(i+1)); 

     i+=2; 
    } 

    if(empty(params.get("txnid"))){ 
     Random rand = new Random(); 
     String rndm = Integer.toString(rand.nextInt())+(System.currentTimeMillis()/1000L); 
     txnid=hashCal("SHA-256",rndm).substring(0,20); 
     popup("txnid : " + txnid); 
     System.out.println("......txnid...... " + txnid); 
    } else { 
     txnid=params.get("txnid"); 
     System.out.println("....else.......txnid...... " + txnid); 
    } 

     //String udf2 = txnid; 
    String txn="abcd"; 
    hash=""; 
    String hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10"; 
    if(empty(params.get("hash")) && params.size()>0) { 
     if(empty(params.get("key")) 
      || empty(params.get("txnid")) 
      || empty(params.get("amount")) 
      || empty(params.get("firstname")) 
      || empty(params.get("email")) 
      || empty(params.get("phone")) 
      || empty(params.get("productinfo")) 
      || empty(params.get("surl")) 
      || empty(params.get("furl")) 
      || empty(params.get("service_provider")) 

    ) { 
      String[] hashVarSeq = hashSequence.split("\\|"); 
      for (int i = 0; i < hashVarSeq.length; i++) { 
       System.out.println("<<<<<>>>>>>>> " + hashVarSeq[i]); 
      } 

      for(String part : hashVarSeq) 
      { 
       hashString= (empty(params.get(part))) ? hashString.concat("") : hashString.concat(params.get(part)); 
       hashString=hashString.concat("|"); 
       System.out.println("??????? " + hashString); 
      } 
      hashString=hashString.concat(salt); 


      hash=hashCal("SHA-512",hashString); 
      System.out.println(hashString + " /////~~~~~~ " + hash); 
      action1=base_url.concat("/_payment"); 
     } 
    } 
    else if(!empty(params.get("hash"))) 
    { 
     hash=params.get("hash"); 
     action1=base_url.concat("/_payment"); 
     System.out.println("!!!!!!!!HASHHHHHHH!!!!!! " + hash); 
    } 

    webView.setWebViewClient(new WebViewClient(){ 


     @Override 
     public void onReceivedError(WebView view, int errorCode, 
       String description, String failingUrl) { 
      // TODO Auto-generated method stub 
      System.out.println(">>>>>>>>>>>>>>onReceivedError>>>>>>>>>>>>>>>>>>"); 
      Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onReceivedSslError(WebView view, 
       SslErrorHandler handler, SslError error) { 
      // TODO Auto-generated method stub 
      System.out.println(">>>>>>>>>>>>>>onReceivedSslError>>>>>>>>>>>>>>>>>>"); 
      Toast.makeText(activity, "SslError! " + error, Toast.LENGTH_SHORT).show(); 
      handler.proceed(); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      // TODO Auto-generated method stub 
      System.out.println(">>>>>>>>>>>>>>shouldOverrideUrlLoading>>>>>>>>>>>>>>>>>>"); 
      return super.shouldOverrideUrlLoading(view, url); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      // TODO Auto-generated method stub 
      super.onPageFinished(view, url); 
      System.out.println(">>>>>>>>>>>>>>onPageFinished>>>>>>>>>>>>>>>>>>"); 
     } 


    }); 


    webView.setVisibility(0); 
    webView.getSettings().setBuiltInZoomControls(true); 
    webView.getSettings().setCacheMode(2); 
    webView.getSettings().setDomStorageEnabled(true); 
    webView.clearHistory(); 
    webView.clearCache(true); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setSupportZoom(true); 
    webView.getSettings().setUseWideViewPort(false); 
    webView.getSettings().setLoadWithOverviewMode(false); 

    webView.addJavascriptInterface(new PayUJavaScriptInterface(activity), "PayUMoney"); 
    Map<String, String> mapParams = new HashMap<String, String>(); 
    mapParams.put("key",merchant_key); 
    mapParams.put("hash",PayMentGateWay.this.hash); 
    mapParams.put("txnid",(empty(PayMentGateWay.this.params.get("txnid"))) ? "" : PayMentGateWay.this.params.get("txnid")); 
    Log.d(tag, "txnid: "+PayMentGateWay.this.params.get("txnid")); 
    mapParams.put("service_provider","payu_paisa"); 

     mapParams.put("amount",(empty(PayMentGateWay.this.params.get("amount"))) ? "" : PayMentGateWay.this.params.get("amount")); 
     mapParams.put("firstname",(empty(PayMentGateWay.this.params.get("firstname"))) ? "" : PayMentGateWay.this.params.get("firstname")); 
     mapParams.put("email",(empty(PayMentGateWay.this.params.get("email"))) ? "" : PayMentGateWay.this.params.get("email")); 
     mapParams.put("phone",(empty(PayMentGateWay.this.params.get("phone"))) ? "" : PayMentGateWay.this.params.get("phone")); 

     mapParams.put("productinfo",(empty(PayMentGateWay.this.params.get("productinfo"))) ? "" : PayMentGateWay.this.params.get("productinfo")); 
     mapParams.put("surl",(empty(PayMentGateWay.this.params.get("surl"))) ? "" : PayMentGateWay.this.params.get("surl")); 
     mapParams.put("furl",(empty(PayMentGateWay.this.params.get("furl"))) ? "" : PayMentGateWay.this.params.get("furl")); 
     mapParams.put("lastname",(empty(PayMentGateWay.this.params.get("lastname"))) ? "" : PayMentGateWay.this.params.get("lastname")); 

     mapParams.put("address1",(empty(PayMentGateWay.this.params.get("address1"))) ? "" : PayMentGateWay.this.params.get("address1")); 
     mapParams.put("address2",(empty(PayMentGateWay.this.params.get("address2"))) ? "" : PayMentGateWay.this.params.get("address2")); 
     mapParams.put("city",(empty(PayMentGateWay.this.params.get("city"))) ? "" : PayMentGateWay.this.params.get("city")); 
     mapParams.put("state",(empty(PayMentGateWay.this.params.get("state"))) ? "" : PayMentGateWay.this.params.get("state")); 

     mapParams.put("country",(empty(PayMentGateWay.this.params.get("country"))) ? "" : PayMentGateWay.this.params.get("country")); 
     mapParams.put("zipcode",(empty(PayMentGateWay.this.params.get("zipcode"))) ? "" : PayMentGateWay.this.params.get("zipcode")); 
     mapParams.put("udf1",(empty(PayMentGateWay.this.params.get("udf1"))) ? "" : PayMentGateWay.this.params.get("udf1")); 
     mapParams.put("udf2",(empty(PayMentGateWay.this.params.get("udf2"))) ? "" : PayMentGateWay.this.params.get("udf2")); 

     mapParams.put("udf3",(empty(PayMentGateWay.this.params.get("udf3"))) ? "" : PayMentGateWay.this.params.get("udf3")); 
     mapParams.put("udf4",(empty(PayMentGateWay.this.params.get("udf4"))) ? "" : PayMentGateWay.this.params.get("udf4")); 
     mapParams.put("udf5",(empty(PayMentGateWay.this.params.get("udf5"))) ? "" : PayMentGateWay.this.params.get("udf5")); 
     mapParams.put("pg",(empty(PayMentGateWay.this.params.get("pg"))) ? "" : PayMentGateWay.this.params.get("pg")); 
     webview_ClientPost(webView, action1, mapParams.entrySet()); 

} 
public class PayUJavaScriptInterface { 
    Context mContext; 

    /** Instantiate the interface and set the context */ 
    PayUJavaScriptInterface(Context c) { 
     mContext = c; 
    } 


    public void success(long id, final String paymentId) { 

    mHandler.post(new Runnable() { 

    public void run() { 

    mHandler = null; 

    Intent intent = new Intent(PayMentGateWay.this, MainActivity.class); 

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    intent.putExtra("result", "success"); 

    intent.putExtra("paymentId", paymentId); 

    startActivity(intent); 

    finish(); 

    } 

    }); 

    } 

} 
public void webview_ClientPost(WebView webView, String url, Collection< Map.Entry<String, String>> postData){ 
     StringBuilder sb = new StringBuilder(); 

     sb.append("<html><head></head>"); 
     sb.append("<body onload='form1.submit()'>"); 
     sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); 
     for (Map.Entry<String, String> item : postData) { 
      sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); 
     } 
     sb.append("</form></body></html>"); 
     Log.d(tag, "webview_ClientPost called"); 
     webView.loadData(sb.toString(), "text/html", "utf-8"); 
} 


public void success(long id, final String paymentId) { 

mHandler.post(new Runnable() { 

public void run() { 

mHandler = null; 

Intent intent = new Intent(PayMentGateWay.this, MainActivity.class); 

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP); 

intent.putExtra(Constants.ACCOUNT_NAME, "success"); 

intent.putExtra(Constants._ID, paymentId); 

startActivity(intent); 

finish(); 

} 

}); 

} 


public boolean empty(String s) 
{ 
    if(s== null || s.trim().equals("")) 
     return true; 
    else 
     return false; 
} 

public String hashCal(String type,String str){ 
    byte[] hashseq=str.getBytes(); 
    StringBuffer hexString = new StringBuffer(); 
    try{ 
    MessageDigest algorithm = MessageDigest.getInstance(type); 
    algorithm.reset(); 
    algorithm.update(hashseq); 
    byte messageDigest[] = algorithm.digest(); 



    for (int i=0;i<messageDigest.length;i++) { 
     String hex=Integer.toHexString(0xFF & messageDigest[i]); 
     if(hex.length()==1) hexString.append("0"); 
     hexString.append(hex); 
    } 

    }catch(NoSuchAlgorithmException nsae){ } 

    return hexString.toString(); 


} 

public void popup(String msg) { 
    Toast.makeText(PayMentGateWay.this, "" + msg, Toast.LENGTH_LONG).show(); 
} 

}

+0

你的意思是發送一個請求到服務器不是嗎?你的webview_ClientPost沒有發佈帖子。 loadUrl只會導致get。如果你解釋了你的問題,那麼有人可能會給你更多的線索。 – e4c5

+0

@ e4c5嗨...發送一個請求到客戶端side.its的服務器工作正常我完成了所有的過程,但在客戶端完成所有的一切後,如何迴應android應用程序的響應。什麼將成爲我的應用程序的成功和失敗的網址。 – jain

+0

你的代碼是目前的形式是不可讀的,但有一些可疑的地方:你真的不想打電話從javascript創建。正如已經提到的webview.loadurl()只是GET,不會發布,你的mapParams只是浪費。 – e4c5

回答

1
  1. 你應該通過自己的成功和失敗的URL(無論該URL可以是相同的)。例如:www.yourwebsitename.com/payuresponse.php
  2. 您可以通過獲得這個職位的參數通過payu發送處理成功或失敗($ _ POST [「狀態」])對你的成功或失敗的網址即payuresponse.php
  3. 添加以下代碼的JavaScript代碼payuresponse.php的底部

    功能成功(){

     Android.success(id,paymentId); 
    
        /*u can pass more parameter too. 
        Note : the number of parameter you pass from here should be  
          same you will recieve in PayMentGateWay.java class in 
          your android application*/ 
        } 
        success(); 
    

    你也可以做任何你想要在你的payuresponse.php

  4. 只需添加ANO在你的Android代碼

@JavascriptInterface

公共無效成功(長ID,最後絃樂paymentId){

/* 塔季翁u能收到更多的參數了。 注意:您這裏收到的參數的數量應該
同你payuresponse.php */

mHandler.post(new Runnable() { 

public void run() { 

mHandler = null; 

Intent intent = new Intent(PayMentGateWay.this, MainActivity.class); 

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP); 

intent.putExtra("result", "success"); 

intent.putExtra("paymentId", paymentId); 

startActivity(intent); 

finish(); 

} 

}); 

} 
發送
  • 希望這會爲你工作。請享用!
  • +0

    @ shahid我不明白你的答案,請參閱我在Java和純原生應用程序中的移動應用程序。我如何給「www.yourwebsitename.com/payuresponse.php」url 。假設我會給我的API方法url.it會響應我在服務器side.so如何獲得在android應用程序的響應。我明白你想說什麼,但我怎麼能在PayMentGateWay活動中調用函數handleResponse()。知道我不明白你的答案,但你能指導我嗎? – jain

    +0

    你的api方法URL(成功和失敗的url)應該在不在本地主機上的活動服務器上,所以payu可以在你的api方法url上打他的迴應。內聯類PayUJavaScriptInterface包含函數success(long id,final String paymentId)將幫助您從api方法url獲得響應。請注意,您的api方法url必須包含()此javascript函數。 – shahid

    相關問題