gpt4 book ai didi

android - android中xml解析的问题

转载 作者:行者123 更新时间:2023-11-29 02:12:24 25 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,我必须在其中将一些数据发布到 url。我进行了谷歌搜索并获得了一些样本。我尝试了它们。但我没有得到所需的响应。

下面是我试过的链接

http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

有没有人做过这个,如果有他能帮我吗

提前致谢图沙尔

最佳答案

我创建了一个类,其中包含我需要发送的所有信息,并将其序列化为带有简单 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;
}

关于android - android中xml解析的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6504821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com