Java.io.FileNotFoundException“URL”

时间:2013-05-22 02:39:00

标签: android

我正在尝试创建和Android应用程序,可以使用主卡api通过信用卡支付。我按照本教程 - >> https://developer.mastercard.com/portal/pages/viewpage.action?pageId=14876685但是它说没有找到网址。

这是我使用的代码:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 

        ((Button) findViewById(R.id.submit)).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Context context = getApplicationContext();
                String endPoint = "https://api.mastercard.com/payments/v1/purchase?Format=XML";
                EditText cardNumberInput = (EditText) findViewById(R.id.cardNumberInput);
                EditText expirationMonthInput = (EditText) findViewById(R.id.expirationMonthInput);
                EditText expirationYearInput = (EditText) findViewById(R.id.expirationYearInput);
                EditText cvvInput = (EditText) findViewById(R.id.cvvInput);
                EditText cardHolderNameInput = (EditText) findViewById(R.id.cardHolderNameInput);
                EditText amountInput = (EditText) findViewById(R.id.amountInput);

                final double amount = Float.valueOf(amountInput.getText().toString());
                final String currency = "USD";
                final String companyId = "ComID";
                final String companyPassword = "ComPass";
                final String messageId = "001";
                final String settlementId = "011";
                final String cardHolderName = cardHolderNameInput.getText().toString();
                final String accountNumber = cardNumberInput.getText().toString();
                final String expiryMonth = expirationMonthInput.getText().toString();
                final String expiryYear = expirationYearInput.getText().toString();
                final String securityCode = cvvInput.getText().toString();
                try {
                    // Send data
                    URL url = new URL(endPoint);
                    URLConnection conn = url.openConnection();
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    OutputStreamWriter request = new OutputStreamWriter(conn.getOutputStream());
                                // Create the XML to post
                    request.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    request.append("<PurchaseRequest>");
                    request.append("<MerchantIdentity>");
                    request.append("<CompanyId>");
                    request.append(companyId);
                    request.append("</CompanyId>");
                    request.append("<CompanyPassword>");
                    request.append(companyPassword);
                    request.append("</CompanyPassword>");
                    request.append("</MerchantIdentity>");
                    request.append("<Reference>");
                    request.append("<MessageId>");
                    request.append(messageId);
                    request.append("</MessageId>");
                    request.append("<SettlementId>");
                    request.append(settlementId);
                    request.append("</SettlementId>");
                    request.append("</Reference>");
                    request.append("<Amount>");
                    request.append("<Currency>");
                    request.append(currency);
                    request.append("</Currency>");
                    request.append("<Value>");
                    request.append(Double.toString(amount));
                    request.append("</Value>");
                    request.append("</Amount>");
                    request.append("<FundingCard>");
                    request.append("<CardholderName>");
                    request.append(cardHolderName);
                    request.append("</CardholderName>");
                    request.append("<AccountNumber>");
                    request.append(accountNumber);
                    request.append("</AccountNumber>");
                    request.append("<ExpiryMonth>");
                    request.append(expiryMonth);
                    request.append("</ExpiryMonth>");
                    request.append("<ExpiryYear>");
                    request.append(expiryYear);
                    request.append("</ExpiryYear>");
                    request.append("<SecurityCode>");
                    request.append(securityCode);
                    request.append("</SecurityCode>");
                    request.append("</FundingCard>");
                    request.append("</PurchaseRequest>");
                    request.flush();

                    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                    Document doc = docBuilder.parse(conn.getInputStream());
                    Node transactionResponseNode = doc.getElementsByTagName("TransactionResponse").item(0);
                    // Test for approval.
                    String response = transactionResponseNode.getNodeValue().trim().toUpperCase();
                    Log.d("**********Response", ""+response);
                    if (response.equals("APPROVED") == true) {
                        request.close();
                        String text = "The payment processed successfully.  PHP" + amountInput.getText().toString()
                                    + " was charged to the account.";
                        Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
                                toast.show();
                    } else if (response.equals("DECLINED")) {
                        String text = "The payment was declined.";
                        Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
                        toast.show();
                    } else if (response.equals("ERROR")) {
                        String text = "Error Processing Payment.";
                        Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
                        toast.show();
                    }
                } catch (MalformedURLException e) {
                    String text = "Error";
                    Toast toast = Toast.makeText(context, ""+e, Toast.LENGTH_LONG);
                    toast.show();
                } catch (IOException e) {
                    String text = "Error";
                    Toast toast = Toast.makeText(context, ""+e, Toast.LENGTH_LONG);
                    toast.show();
                } catch (ParserConfigurationException e) {
                    String text = "Error";
                    Toast toast = Toast.makeText(context, ""+e, Toast.LENGTH_LONG);
                    toast.show();
                } catch (SAXException e) {
                    String text = "Error";
                    Toast toast = Toast.makeText(context, ""+e, Toast.LENGTH_LONG);
                    toast.show();
                }
            }
        });
    }
}

任何想法都会对我有所帮助。感谢。

这就是log cat所说的:

05-22 11:20:58.129: D/**********Error2(1424): java.io.FileNotFoundException: https://api.mastercard.com/payments/v3/purchase?Format=XML

1 个答案:

答案 0 :(得分:0)

- 在UI线程上执行 UI工作,在非UI线程上非UI工作总是更好,但是Android's Honeycomb版本为法律。

- 使用后台主题进行网络通话。

- 使用Thread进行网络调用,然后使用Handler将数据放在用户界面上,或者使用所谓的无痛线程< / strong>在Android中,AsyncTask