Thursday, February 6, 2014

Android calling webservice returns dataset types, can not be resolved


            
/ / Namespace
String nameSpace = "http://tempuri.org/";
/ / Method name
callString methodName = "Getlist";
/ / EndPoint
String endPoint = "http://10.141.37.197/yy/AndroidService.asmx";
/ / SOAP Action
String soapAction = "http://tempuri.org/Getlist";

/ / Specify WebService namespace and method name to call
SoapObject rpc = new SoapObject (nameSpace, methodName);

/ / Set the required incoming call WebService interface requires two parameters mobileCode, userId
rpc.addProperty ("id", phoneSec);
/ / Rpc.addProperty ("userId", "");

/ / Generate method call WebService SOAP request message, and specify the version of SOAP
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (
SoapEnvelope.VER10);

envelope.bodyOut = rpc;
/ / Set whether the call is. Net development WebService
envelope.dotNet = true ;/ / / / webservice specified type (java, PHP, dotNet)

/ / equivalent to envelope.bodyOut = rpc;
envelope.setOutputSoapObject (rpc);

HttpTransportSE transport = new HttpTransportSE (endPoint);
try {
/ / Call WebService
transport.call (soapAction, envelope);
} Catch (Exception e) {
e.printStackTrace ();
}

/ / Get the returned data
SoapObject object = (SoapObject) envelope.bodyIn ;/ / error here, a direct jump out because webservice client returns a dateset / / Get Return results
String result = object.getProperty (0) toString ();.

/ / Display the results returned by WebService in TextView in
textView.setText (result);
Reply:

This is the type of return, make up, how to resolve it?
Reply:
Do not sink ah, 555555555555
Reply:
Recently, too anxious to do it, you marching help
Reply:
Can not resolve it yourself?
Reply:
You all know that the return type is DataSet, and how still use SoapObject and (SoapObject) do convert it?
Use DataSet treatment should be all right.
Reply:
Xml parsing can not directly get the element sequence under
Reply:
You use system.out.println (object.toString ()) look, your screenshot looks like a WebService method provides not return data.
I here call returns the following data:
06-18 17:57:04.730: I / System.out (29365): anyType {item = N / A; item = 5703918.7; item = 227986;}

Reply:
Another tragedy webservice communication with donet android platform

Two solutions:
1, so that the webservice return donet platform standard xml object. Then get android side parsing XML tree JAVA objects.

2, android terminal directly parse tree assembled JAVA DataSet object.

Another: Get return results, please use: envelope.getResponse ();
Reply:
Finally, put the data they used are taken out,
/ / Use Map to represent tabular data
Map mapOne = new HashMap ();

flags = object.getProperty (0) toString ();.
Pattern pattern = Pattern.compile ("ds = anyType \ \ {[^}] * \ \};");

Matcher m = pattern.matcher (flags) ;/ / import java.util.regex.Matcher;


while (m.find ())
{

String tmp = m.group () replace ("ds = anyType {", "");.
String [] Strlen = tmp.split (";");

if (Strlen.length> 1)
{
mapOne = new HashMap (); case / / just create a new map, you can prevent the value of the covered
. String khbh = Strlen [0] split ("=") [1];
. String khqc = Strlen [1] split ("=") [1];
mapOne.put ("user_name", khbh);
mapOne.put ("user_pasword", khqc) ;/ / map keys are not allowed to repeat, otherwise the previous value will be overwritten
list.add (mapOne);
}

}
Reply:
Also encountered a big problem, I have Android 2.2 phones can access the network, but the development of software version 4.0 or higher installed directly error (4.0 after the main thread can not access the network), the online approach seems to not work, If you want to change, and that the above code on how to change it?
Reply:
1 mistake that step should be written like this:
if (envelope.getResponse ()! = null) {
return parse (envelope.bodyIn.toString ());
}

/ **************
* Parse XML
* @ Param str
* @ Return
* /
private static List parse (String str) {
String temp;
List list = new ArrayList ();
if (str! = null && str.length ()> 0) {
int start = str.indexOf ("string");
int end = str.lastIndexOf (";");
temp = str.substring (start, end-3);
String [] test = temp.split (";");

for (int i = 0; i if (i == 0) {
temp = test [i] substring (7);.
} Else {
temp = test [i] substring (8);.
}
int index = temp.indexOf (",");
list.add (temp.substring (0, index));
}
}
return list;
}

2 colon is xml namespace, self Baidu: xml parsing namespace lookup

3.4.0 After the main thread can not access the network, which is strict mode, StrictMode class. Baidu own strict mode
Reply:
package test.webservice;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import test.webservice.R;
import android.os.Bundle;
import android.os.Handler;

import android.os.StrictMode;

import android.os.AsyncTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ListView;
import android.os.PatternMatcher;

import java.util.Iterator;
import java.util.Set;
import java.util.List;

import android.app.ListActivity;
import android.graphics.Color;

import java.util.ArrayList;
import java.util.HashMap;

import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;


import android.net.NetworkInfo;
import android.os.Build.VERSION;

public class kehu extends Activity {

EditText editText1;
TextView textView1;
Button button1;
ListView lv;

ArrayList > list = new ArrayList > ();
public static String flags = "";
private SimpleAdapter listAdapter;
public static String phoneNum = "";
@ Override
public void onCreate (Bundle savedInstanceState) {


super.onCreate (savedInstanceState);
setContentView (R.layout.kehu);



editText1 = (EditText) findViewById (R.id.editText1);
textView1 = (TextView) findViewById (R.id.textView1);
button1 = (Button) findViewById (R.id.button1);
lv = (ListView) findViewById (R.id.ListView_detail);


textView1.setText ("Please enter the customer's name!");




button1.setOnClickListener (new View.OnClickListener () {
@ Override
public void onClick (View v) {
phoneNum = editText1.getText () toString () trim ();..
getRemoteInfo (phoneNum);

}

public void getRemoteInfo (String phoneSec) {


/ / Namespace
String nameSpace = "http://tempuri.org/";
/ / Method name
callString methodName = "GetListfhkh";
/ / EndPoint
String endPoint = "http://10.141.37.197/AndroidServiceS/AndroidService.asmx";
/ / SOAP Action
String soapAction = "http://tempuri.org/GetListfhkh";

/ / Specify WebService namespace and method name to call
SoapObject rpc = new SoapObject (nameSpace, methodName);

/ / Set the required incoming call WebService interface requires two parameters mobileCode, userId
rpc.addProperty ("khlxr", phoneSec);

/ / Generate method call WebService SOAP request message, and specify the version of SOAP
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope.VER11);

envelope.bodyOut = rpc;
/ / Set whether the call is. Net development WebService
envelope.dotNet = true ;/ / / / webservice specified type (java, PHP, dotNet)

/ / Equivalent to envelope.bodyOut = rpc;
envelope.setOutputSoapObject (rpc);

HttpTransportSE transport = new HttpTransportSE (endPoint);
try {
/ / Call WebService
transport.call (soapAction, envelope);
} Catch (Exception e) {
e.printStackTrace ();
}

/ / Get the returned data
SoapObject object = (SoapObject) envelope.bodyIn ;/ / error here because the webservice client returns a dataset
/ / Get the results back, and here to add the data returned is an integer type, can also be parsed out, is dataset can not be properly resolved, delete operation, the returns boolean true, can correctly parse

/ / Http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx?op=getDomesticAirlinesTime, this is the return dataset of data, this can be solved if we can solve the above problem

/ / Use Map to represent tabular data
Map mapOne = new HashMap ();

flags = object.getProperty (0) toString ();.
Pattern pattern = Pattern.compile ("ds = anyType \ \ {[^}] * \ \};");

Matcher m = pattern.matcher (flags) ;/ / import java.util.regex.Matcher;


while (m.find ())
{

String tmp = m.group () replace ("ds = anyType {", "");.
String [] Strlen = tmp.split (";");

if (Strlen.length> 1)
{
mapOne = new HashMap (); case / / just create a new map, you can prevent the value of the covered
. String khbh = Strlen [0] split ("=") [1];
. String khqc = Strlen [1] split ("=") [1];
mapOne.put ("user_name", khbh);
mapOne.put ("user_pasword", khqc) ;/ / map keys are not allowed to repeat, otherwise the previous value will be overwritten
list.add (mapOne);
}

}

String title = "Query results are as follows:";
textView1.setText (title);
listAdapter = new SimpleAdapter (this, list, R.layout.user, new String [] {"user_name", "user_pasword"}, new int [] {R.id.user_name, R.id.user_pasword});
/ / Set the display ListView
lv.setAdapter (listAdapter);

}


}



The above code testing on android 2.2 by the real machine, but can not be tested in 4.0 through (the main thread can not access the network), are there any expert can help to modify, so that it can also be able to access the network in version 4.0 or higher.


Reply:
cited a floor tang0575 reply:

This is the type of return, make up, how to resolve it?

Would like to ask you this question on your side to solve it? I also encountered the same problem, have a good tune for a few days or not. The total error
SoapFault - faultcode: 'soap: Client' faultstring: 'Server not recognize the value of HTTP Header SOAPAction:. 'Faultactor:' null 'detail: org.kxml2.kdom.Node @ 4056e118

No comments:

Post a Comment