Saturday, January 25, 2014

android call c # web service to achieve the sd card txt file data is written to a remote database


            
c # web service code:

After running
android-side code:
public class Upload extends Activity {
private static String requestURL = "http://192.168.120.163/WebServiceLearn/Service1.asmx";
private static final String methodName = "UploadAndroidFile";
. private String srcUrl = Environment.getExternalStorageDirectory () getAbsolutePath () ;/ / get SDCard directory;
private String fileName = "firstcheckinfo.txt";
private String picPath = srcUrl + "/" + fileName;
private List checkinfos = new ArrayList ();
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
/ / SetContentView (R.layout.upload);
try {
FileInputStream fis = new FileInputStream (picPath);
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
byte [] buffer = new byte [1024];
int count = 0;
while ((count = fis.read (buffer))> = 0) {
baos.write (buffer, 0, count);
}
String uploadBuffer = new String (Base64.encode (baos.toByteArray (), count)); / / perform Base64 encoding
List connectstring = connectWebService (fileName); / / call the webservice

fis.close ();
} Catch (Exception e) {
e.printStackTrace ();
}



}
private List connectWebService (String fileName2) {
/ / TODO Auto-generated method stub
/ / Use ksoap2 call webservice
String namespace = "http://localhost/WebServiceLearn";
/ / Namespace that the server was interfaces Note: suffix did not add wsdl,
.
/ / Server side I am using c # to achieve webservice interfaceString url = "http://localhost/WebServiceLearn/Service1.asmx?wsdl";
/ / SOAP Action
String soapAction = "http://localhost/WebServiceLearn/UploadAndroidFile";


/ / Specify WebService namespace and method name to call
SoapObject soapObject = new SoapObject (namespace, methodName);
soapObject.addProperty ("FileName", fileName2); / / parameter a file name
/ / Generate method call WebService SOAP request message. This information is described by SoapSerializationEnvelope object
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope.VER10);
envelope.bodyOut = soapObject;
. (New MarshalBase64 ()) register (envelope);
envelope.dotNet = true; / / This property is supported dotnetwebservice attributes, if you do not specify rpc dotnetwebservice way, true, or is false

envelope.setOutputSoapObject (soapObject);

/ / Instantiate a HttpTransportSE object to call web service, and get the xml string data
HttpTransportSE httpTranstation = new HttpTransportSE (url);
httpTranstation.debug = true ;/ / if it is in debug mode

try
{
httpTranstation.call (soapAction, envelope);
SoapObject result = (SoapObject) envelope.getResponse ();
/ / The following results were parsed structure similar to json object
int count = result.getPropertyCount ();
for (int index = 0; index
checkinfos.add (result.getProperty (index) toString ().);
}
} Catch (IOException e) {
e.printStackTrace ();
} Catch (XmlPullParserException e)
{
e.printStackTrace ();
}

return checkinfos;
}

} Each run to SoapObject result = (SoapObject) envelope.getResponse (); went IOException e Department reported SoapFault - faultcode: 'soap: Client' faultstring: 'Server does not recognize the value of HTTP Header SOAPAction:. 'Faultactor:' null 'detail: org.kxml2.kdom.Node @ 4056e118 error, try a variety of methods will not work, very anxious, heroes who help to look at, very grateful.
Reply:
You can try to visit this part of the code WEBSERVICE, put thread calls;
Sometimes android version higher. Visit INTENT need to thread access.
Reply:
cited a floor dl_lzx reply:
you can try to visit WEBSERVICE this part of the code, put thread calls;
Sometimes android version higher. Visit INTENT need to thread access.
My web service returns a dataset, android in then parse it? This issue is full for several days, have a big head, big God help me find it.
Reply:
reference to the second floor lhx200610930202 reply:
Quote: references to a floor dl_lzx reply:

You can try to visit this part of the code WEBSERVICE, put thread calls;
Sometimes android version higher. Visit INTENT need to thread access.
My web service returns a dataset, android in then parse it? This issue is full for several days, have a big head, big God help me find it.


WS client should not return dataset, it seems like android terminal handling problems;
You should convert WS client dataset remove data into JSON data format or XML data format, and then return to the android side, so that resolve relatively simpler.
Reply:
reference to the third floor dl_lzx reply:
Quote: references to the second floor lhx200610930202 reply:

Quote: references to a floor dl_lzx reply:

You can try to visit this part of the code WEBSERVICE, put thread calls;
Sometimes android version higher. Visit INTENT need to thread access.
My web service returns a dataset, android in then parse it? This issue is full for several days, have a big head, big God help me find it.


WS client should not return dataset, it seems like android terminal handling problems;
You should convert WS client dataset remove data into JSON data format or XML data format, and then return to the android side, so that resolve relatively simpler.

At present, the problem has been solved, the solution posted for newcomers learn:
After 1.web service returns a value of type string, which parameters (string Filename, byte [] data) which byte [] is passed over from the android in a byte array, filename is passed to upload files on the server names.
2, in the android side, String uploadBuffer = new String (Base64.encode (baos.toByteArray (), count)); / / perform Base64 encoding code to remove this one, plus buffer = baos.toByteArray ();
connectWebServicetwo (fileName, buffer);
3 In connectWebServicetwo function, try catch in to try
{
httpTranstation.call (soapAction, envelope);
org.ksoap2.serialization.SoapPrimitive so = (org.ksoap2.serialization.SoapPrimitive) envelope.getResponse ();
/ / Object so = (Object) envelope.bodyIn;
ret = Boolean.valueOf (so.toString ());
return ret;


} Catch (Exception e) {
e.printStackTrace ();
}
Red font is a key part. After this modification to normal operation. I am grateful enthusiastic users answer.

No comments:

Post a Comment