我在我的android应用程序中有以下活动,它创建了一个带有身份验证的httpsurlconnection。然后它发布一些XML并捕获响应。我从服务器收到的响应是没有收到XML消息,但日志中没有例外。
我测试了与Java应用程序相同的代码,它运行正常。我已经搜索了几个星期的高低,但似乎无法找到答案。
我在我的清单文件中设置了android.permission.INTERNET。
我们非常感谢任何帮助,我在下面发布了我的代码减去连接中使用的用户名和密码。
的活动:
package com.payments.WorldPay;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class OrderModification extends Activity
{
TextView resultMessage;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.order_modification);
Button submit = (Button)findViewById(R.id.submitButton);
submit.setOnClickListener(submitModification);
resultMessage = (TextView)this.findViewById(R.id.result);
}
OnClickListener submitModification = new OnClickListener()
{
public void onClick(View v)
{
StringBuffer xmlMod = new StringBuffer();
xmlMod.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xmlMod.append("<!DOCTYPE paymentService PUBLIC \"-//RBS WorldPay/DTD RBS WorldPay
PaymentService v1//EN\" \"http://dtd.wp3.rbsworldpay.com/paymentService_v1.dtd\">");
xmlMod.append("<paymentService version=\"1.4\" merchantCode=\"TECHSUPPORT\">");
xmlMod.append("<modify>");
xmlMod.append("<orderModification orderCode=\"123456789\">");
xmlMod.append("<cancel/>");
xmlMod.append("</orderModification>");
xmlMod.append("</modify>");
xmlMod.append("</paymentService>");
SendXml sendMod = new SendXml(xmlMod.toString());
resultMessage.setText(sendMod.submitOrder());
}
};
}
连接类:
package com.payments.WorldPay;
import java.io.*;
import java.net.*;
import javax.net.ssl.HttpsURLConnection;
public class SendXml
{
protected final static String merchantCode="username";
protected final static String xmlPassword="password";
protected final static String envUrl="https://secure-test.wp3.rbsworldpay.com/jsp/merchant/xml/paymentService.jsp";
protected String xmlRequest;
public SendXml(String xmlRequest)
{
this.xmlRequest = xmlRequest;
}
public String submitOrder()
{
HttpsURLConnection conn = null;
try
{
System.setProperty("http.keepAlive", "false");
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL(envUrl);
conn = (HttpsURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setConnectTimeout(30000);
String utf8Xml = URLEncoder.encode(xmlRequest, "UTF-8");
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(utf8Xml);
wr.flush();
wr.close();
InputStream response;
if(conn.getResponseCode()==200)
{
response = conn.getInputStream();
}
else
{
response = conn.getErrorStream();
}
BufferedReader in = new BufferedReader(new InputStreamReader(response),4800);
StringBuffer responseBuffer = new StringBuffer();
String line;
while ((line = in.readLine()) != null)
{
responseBuffer.append(line);
}
in.close();
return responseBuffer.toString();
}
catch(Exception e)
{
return("Connection Error: "+e);
}
finally
{
conn.disconnect();
}
}
public static class MyAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return (new PasswordAuthentication(merchantCode, xmlPassword.toCharArray()));
}
}
}
我已经将代码再次作为Java项目运行,它运行正常。我在下面列出了示例请求和响应。
请求:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//RBS WorldPay/DTD RBS WorldPay PaymentService v1//EN"
"http://dtd.wp3.rbsworldpay.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="TECHSUPPORT">
<modify>
<orderModification orderCode="123456789">
<cancel/>
</orderModification>
</modify>
</paymentService>
响应:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//Bibit//DTD Bibit PaymentService v1//EN"
"http://dtd.bibit.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="TECHSUPPORT">
<reply>
<ok>
<cancelReceived orderCode="123456789"/>
</ok>
</reply>
</paymentService>
如果我只是删除&gt;从上面的元素我将收到以下错误。
错误回复:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//Bibit//DTD Bibit PaymentService v1//EN"
"http://dtd.bibit.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="TECHSUPPORT">
<reply>
<error code="2">
<![CDATA[Element type "modify" must be followed by either attribute specifications, ">" or
"/>".]]>
</error>
</reply>
</paymentService>
提前致谢 达伦
答案 0 :(得分:3)
我没有找到问题的答案,但下面的代码允许我成功发布我的XML。
public ArrayList<PaymentResult> submitOrder()
{
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(envUrl);
UsernamePasswordCredentials login = new UsernamePasswordCredentials(merchantCode, xmlPassword);
StringBuffer responseBuffer = null;
try
{
request.addHeader(BasicScheme.authenticate(login,"UTF-8",false));
StringEntity xmlmessage = new StringEntity(xmlRequest,HTTP.UTF_8);
xmlmessage.setContentType("text/xml");
request.setHeader("Content-Type","text/xml;charset=UTF-8");
request.setEntity(xmlmessage);
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
responseBuffer = new StringBuffer();
String line;
while ((line = in.readLine()) != null)
{
responseBuffer.append(line);
}
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return parseResponse(responseBuffer.toString());
}