gpt4 book ai didi

android - org.apache.http.client.HttpResponseException : Internal Server Error while posting xml String to server

转载 作者:搜寻专家 更新时间:2023-11-01 08:13:17 24 4
gpt4 key购买 nike

我正在使用 httpClient#execute(HttpPost, ResponseHandler) 向我的 Web 应用程序发送一个 xml 字符串

当字符串只包含一个标签时,它工作正常,但如果两个相同的标 checkout 现在根标签之后,它会返回内部服务器错误。例如:

<Class>
<Student>Some Elements and Attributes</Student>
<Student>Some Elements and Attributes</Student>
</Class>

我的代码在这里用于创建 xml 字符串:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

//root element
Document document = docBuilder.newDocument();
Element rootElement = document.createElement("Class");
document.appendChild(rootElement);

Element student = document.createElement("Student");
rootElement.appendChild(student);

Attr name = document.createAttribute("Name");
name.setValue("");
student.setAttributeNode(name);

//Courses
Element courses = document.createElement("Courses");
Element course = document.createElement("Course");
Attr name = document.createAttribute("Name");
name.setValue("any name");
level.setAttributeNode(name);
courses.appendChild(course);
student.appendChild(courses);

// write the content into xml string
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
Source source = new DOMSource(document);
transformer.transform(source, result);
writer.close();
xml = writer.toString();

这里是发送xml字符串的代码:

String url = "My URL string";

DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler<String> res = new BasicResponseHandler();

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1", "student name"));
nameValuePairs.add(new BasicNameValuePair("param2", xmlString));

HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

String response = httpClient.execute(httpPost, res);

此代码正在产生问题。 StackTrace 在这里:

09-21 00:58:29.486: WARN/System.err(7450): org.apache.http.client.HttpResponseException: Internal Server Error
09-21 00:58:29.490: WARN/System.err(7450): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
09-21 00:58:29.494: WARN/System.err(7450): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
09-21 00:58:29.497: WARN/System.err(7450): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
09-21 00:58:29.501: WARN/System.err(7450): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
09-21 00:58:29.501: WARN/System.err(7450): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
09-21 00:58:29.505: WARN/System.err(7450): at com.example.httpandroidtutorial.HttpAndroidTutorialActivity.onClick(HttpAndroidTutorialActivity.java:122)
09-21 00:58:29.509: WARN/System.err(7450): at android.view.View.performClick(View.java:2461)
09-21 00:58:29.513: WARN/System.err(7450): at android.view.View$PerformClick.run(View.java:8888)
09-21 00:58:29.517: WARN/System.err(7450): at android.os.Handler.handleCallback(Handler.java:587)
09-21 00:58:29.517: WARN/System.err(7450): at android.os.Handler.dispatchMessage(Handler.java:92)
09-21 00:58:29.521: WARN/System.err(7450): at android.os.Looper.loop(Looper.java:123)
09-21 00:58:29.525: WARN/System.err(7450): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-21 00:58:29.529: WARN/System.err(7450): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 00:58:29.533: WARN/System.err(7450): at java.lang.reflect.Method.invoke(Method.java:521)
09-21 00:58:29.533: WARN/System.err(7450): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-21 00:58:29.537: WARN/System.err(7450): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-21 00:58:29.540: WARN/System.err(7450): at dalvik.system.NativeStart.main(Native Method)

这些 HttpResponseExceptions 的原因是什么?

最佳答案

错误来自您的服务器。 XML 可能生成正确,但服务器在获取它时遇到错误。您将需要调试 Web 应用程序以找出问题所在。

希望这对您有所帮助。

关于android - org.apache.http.client.HttpResponseException : Internal Server Error while posting xml String to server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7483203/

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