我正在设计一个需要经常连接到Web服务的应用程序。每当我使用它时它都可以正常工作但是如果我让它暂时搁置一段时间并制作一些咖啡然后回来尝试去参加需要连接到Web服务的活动,它不会work和我的服务响应返回null。
是否有任何我可能遗漏的代码(Android开发新手),这使得手机保持连接状态。但是,我有一个按钮,打开一个Web浏览器来打开一个链接,似乎随时可以工作。因此,我认为这与互联网连接无关。
根据要求在此处编码:
private String getIndividualProduct() throws IOException, XmlPullParserException {
String data = null;
String methodname = "getProductById";
SoapObject request = new SoapObject(NAMESPACE, methodname);
request.addProperty("id", id);
request.addProperty("shopId", shopId);
SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
HttpTransportSE ht = getHttpTransportSE();
MarshallDouble marshall = new MarshallDouble();
marshall.register(envelope);
ht.call(SOAP_ACTION, envelope);
Object resultsString = (Object)envelope.getResponse();
data = resultsString.toString();
return data;
}
private static SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
return envelope;
}
private static HttpTransportSE getHttpTransportSE() throws IOException {
HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY, MAIN_REQUEST_URL, 60000);
ht.debug = true;
ht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
return ht;
}
这是我的活动代码:
private ArrayList<Product> readFile() throws Exception {
ArrayList<Product> products = new ArrayList<Product>();
FileReader in = new FileReader(cart);
BufferedReader reader = new BufferedReader(in);
String temp;
String data = null;
while ((temp = reader.readLine()) != null) {
try {
WebService service = new WebService(temp, true);
data = service.getValue();
} catch (Exception e) {
this.runOnUiThread(new Runnable() {
public void run() {
Toast no_cnx = Toast.makeText(
Winkelwagen_activity.this,
"Error Connecting. Try again.",
Toast.LENGTH_SHORT);
no_cnx.setGravity(Gravity.CENTER, 0, 0);
no_cnx.show();
Intent back = new Intent(Winkelwagen_activity.this,
Main_activity.class);
startActivity(back);
Winkelwagen_activity.this.finish();
}
});
}
JsonParser parser = new JsonParser(data, shopId, true);
Product product = parser.getSingleProduct();
if (product != null) {
products.add(product);
}
}
reader.close();
in.close();
return products;
}