C# サーバーから XML を送信し、Android Java クライアントで受信する
受信した XML は次のようになります。
<?xml version="1.0" encoding="utf-8"?>.....
これはC#送信コードです
// convert the class WorkItem to xml
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(WorkItem));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, p);
// send the xml version of WorkItem to client
byte[] data = memoryStream.ToArray();
clientStream.Write(data, 0, data.Length);
Console.WriteLine(" send.." + data);
clientStream.Close();
Javaでは、次のようにします:
in = new DataInputStream(skt.getInputStream());
String XMlString = in.readLine();
から最初の3文字を毎回削除すると、すべてが機能しXMlString
ます。
可能であれば、これをより良い方法で行いたいと思います
*Android Java クライアントを追加する更新
@Override
protected String doInBackground(Long... params) {
textTopInfo.setText("Loading workitems..");
DataOutputStream out = null;
DataInputStream in = null;
try {
Socket skt = new Socket(Consts.SERVER_URL_1, Consts.SERVER_PORT_1);
skt.setSoTimeout(10000); //10 sec timout
out = new DataOutputStream(skt.getOutputStream());
in = new DataInputStream(skt.getInputStream());
// check valid user id
String id = prefs.getString("id", "");
if(id.equals(""))
return "Open menu and enter User Id";
String theString = Consts.PUSH_GET_WORKITEM + ":" + id ;
out.write(theString.getBytes());
BufferedReader d = new BufferedReader
(new InputStreamReader(skt.getInputStream()));
String XMlString = d.readLine();
// here I remove the BOM
XMlString = XMlString.substring(3);
Log.d(TAG, "GF");
XStream xstream = new XStream();
xstream.alias("WorkItem", WorkItem.class);
xstream.alias("OneItem", OneItem.class);
pl = (WorkItem)xstream.fromXML(XMlString);
} catch (Exception e) {
return "cannot connect to server " + e.toString();
}finally{
//kill out/in
try {
if(out != null)
out.close();
if(in!=null)
in.close();
} catch (IOException e) {
}
}
return "here is the list";
}