xml解析在android中的问题
问题描述:
我正在开发一个android应用程序,我必须发布一些数据到url.I没有使用谷歌搜索和一些samples.I尝试了他们。但我没有得到响应所需。xml解析在android中的问题
下面是我试图
http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
链接有没有人做到了这一点before.If是的,他可以帮我
在此先感谢 图莎尔
答
我创建一个类我需要发送的所有信息并使用简单的XML将其序列化为xml文件。然后我发送整个XML。 但你也可以只发送字段:
static String CRLF = "\r\n";
static String twoHyphens = "--";
static String boundary = "*****mgd*****";
private DataOutputStream dataStream = null;
private void postData(){
try{
URL connectURL = new URL("http://example.com/upload.php");
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "test");
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary);
conn.connect();
dataStream = new DataOutputStream(conn.getOutputStream());
//Send fields
writeFormField("name", "bla");
writeFormField("password", "bla");
//Send a file
File uploadFile = new File(SD_CARD_TEMP_DIR, "file.xml");
FileInputStream fileInputStream = new FileInputStream(uploadFile);
writeFileField("myFile", "somefilename.xml", "text/xml",fileInputStream);
// final closing boundary line
dataStream.writeBytes(twoHyphens + boundary +twoHyphens + CRLF);
fileInputStream.close();
dataStream.flush();
dataStream.close();
dataStream = null;
boolean response = getResponse(conn);
if(response)
{
finish();
}
}
catch (MalformedURLException mue) {
// TODO
}
catch (IOException ioe) {
// TODO
}
catch (Exception e) {
// TODO
}
}
private void writeFormField(String fieldName, String fieldValue) {
try {
dataStream.writeBytes(twoHyphens + boundary + CRLF);
dataStream.writeBytes("Content-Disposition: form-data;name=\"" + fieldName + "\"" + CRLF);
dataStream.writeBytes(CRLF);
dataStream.writeBytes(fieldValue);
dataStream.writeBytes(CRLF);
} catch (Exception e) {
System.out.println("writeFormField:got: " + e.getMessage());
}
}
private void writeFileField(
String fieldName,
String fieldValue,
String type,
FileInputStream fis)
{
try
{
// opening boundary line
dataStream.writeBytes(twoHyphens + boundary + CRLF);
dataStream.writeBytes("Content-Disposition: form-data; name=\""
+ fieldName
+ "\";filename=\""
+ fieldValue
+ "\""
+ CRLF);
dataStream.writeBytes("Content-Type: " + type + CRLF);
dataStream.writeBytes(CRLF);
// create a buffer of maximum size
int bytesAvailable = fis.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fis.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dataStream.write(buffer, 0, bufferSize);
bytesAvailable = fis.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fis.read(buffer, 0, bufferSize);
}
// closing CRLF
dataStream.writeBytes(CRLF);
}
catch(Exception e)
{
Log.i(TAG, "writeFormField: got: " + e.getMessage());
}
}
private boolean getResponse(HttpURLConnection conn)
{
try {
// try doing this in one read
InputStream data = conn.getInputStream();
StringBuffer sb = new StringBuffer();
//Reader reader = new InputStreamReader(data, "UTF-8");
int c;
while ((c = data.read()) != -1) sb.append((char) c);
String document = sb.toString();
int responseCode = conn.getResponseCode();
return true;
}
catch(Exception e)
{
}
return false;
}
请看下面的链接对你更有帮助。 http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser – Nikhil