org.apache.http.client.HttpResponseException:将xml字符串发布到服务器时发生内部服务器错误

时间:2011-09-20 09:47:57

标签: android xml httpclient

我使用 httpClient #cute(HttpPost,ResponseHandler)

将xml字符串发送到我的Web应用程序

当字符串只包含一个标记时,它可以正常工作,但如果两个相同的标记位于根标记之后,它将返回内部服务器错误。例如:

<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的原因是什么?

2 个答案:

答案 0 :(得分:3)

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

希望这有帮助。

答案 1 :(得分:1)

我有类似的问题。在我的情况下,服务器没有正确处理POST请求。当我改为GET时,异常没有到来。