【问题标题】:Android - send and receive XML using HTTPPOSTAndroid - 使用 HTTPPOST 发送和接收 XML
【发布时间】:2013-03-14 18:34:35
【问题描述】:

你好!

我知道这个问题及其版本已经在 SO 上多次提出,并且我已经阅读了我能找到的关于这个问题的所有线程。我已经尝试了我能找到的所有解决方案组合,但不知何故,我仍然遇到问题。我希望您不会介意我创建自己的问题,以便我可以发布我的特定代码。我对 Android 应用完全陌生,并且有一些基本的 Java 背景。

要定义一些变量...

sendXML 是一个字符串,它是我的 XML 代码。

httppost 是我发布到的 URL。

points 是一个 TextView,我最终会根据服务器的响应 XML 进行更新。

    public void postTo() {

    try {
        StringEntity entity = new StringEntity(sendXML,HTTP.UTF_8);
        entity.setContentType("text/xml");
        httppost.setEntity(entity);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity responseEntity= response.getEntity();
        points.setText(EntityUtils.toString(responseEntity));

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        points.setText("SomeMessageHere");
        e.printStackTrace();
    } catch (IOException q) {
        // TODO Auto-generated catch block
        points.setText("SomeMessageHere");
        q.printStackTrace();

    }

}

每当我尝试单击应用程序中调用 postTo() 的按钮时,我的整个应用程序都会崩溃。任何帮助将不胜感激!

Logcat 如下:

03-14 19:03:13.761: E/AndroidRuntime(922): FATAL EXCEPTION: main
03-14 19:03:13.761: E/AndroidRuntime(922): java.lang.IllegalArgumentException: Source     string may not be null
03-14 19:03:13.761: E/AndroidRuntime(922):  at org.apache.http.entity.StringEntity.<init>(StringEntity.java:59)
03-14 19:03:13.761: E/AndroidRuntime(922):  at  com.appname.app.Stash.postTo(Stash.java:136)
03-14 19:03:13.761: E/AndroidRuntime(922):  at com.appname.app.Stash$1.onClick(Stash.java:97)
03-14 19:03:13.761: E/AndroidRuntime(922):  at android.view.View.performClick(View.java:4204)
03-14 19:03:13.761: E/AndroidRuntime(922):  at android.view.View$PerformClick.run(View.java:17355)
03-14 19:03:13.761: E/AndroidRuntime(922):  at android.os.Handler.handleCallback(Handler.java:725)
03-14 19:03:13.761: E/AndroidRuntime(922):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 19:03:13.761: E/AndroidRuntime(922):  at android.os.Looper.loop(Looper.java:137)
03-14 19:03:13.761: E/AndroidRuntime(922):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-14 19:03:13.761: E/AndroidRuntime(922):  at java.lang.reflect.Method.invokeNative(Native Method)
03-14 19:03:13.761: E/AndroidRuntime(922):  at java.lang.reflect.Method.invoke(Method.java:511)
03-14 19:03:13.761: E/AndroidRuntime(922):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-14 19:03:13.761: E/AndroidRuntime(922):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-14 19:03:13.761: E/AndroidRuntime(922):  at dalvik.system.NativeStart.main(Native Method)
03-14 19:03:13.810: W/ActivityManager(297):   Force finishing activity com.appname.app/.Stash
03-14 19:03:13.860: W/WindowManager(297): Failure taking screenshot for (328x583) to layer 21015
03-14 19:03:14.360: I/Choreographer(297): Skipped 30 frames!  The application may be doing too much work on its main thread.
03-14 19:03:14.390: W/ActivityManager(297): Activity pause timeout for ActivityRecord{411022b8 u0 com.appname.app/.Stash}
03-14 19:03:14.390: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property

下面是一个额外的 logcat 打印出 sendXML 字符串的值。我已经尝试过在字符串的开头包含和不包含 xml version="1.0" 。我不确定哪个是正确的。 XMLNS="URLHERE" 当然有正确的 URL 发布到:

03-14 23:53:26.870: I/myApp(1048): sendXML = <?xml version="1.0"?><EmailRequest xmlns="URLHERE"><Hash>5e1da0407254ec8882c6f6c27385a890ef544b32</Hash></EmailRequest>
03-14 23:53:26.970: D/AndroidRuntime(1048): Shutting down VM
03-14 23:53:26.970: W/dalvikvm(1048): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-14 23:53:26.990: E/AndroidRuntime(1048): FATAL EXCEPTION: main
03-14 23:53:26.990: E/AndroidRuntime(1048): android.os.NetworkOnMainThreadException
03-14 23:53:26.990: E/AndroidRuntime(1048):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at com.myapp.app.Stash.postTo(Stash.java:140)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at com.mymapp.app.Stash$1.onClick(Stash.java:96)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at android.view.View.performClick(View.java:4204)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at android.view.View$PerformClick.run(View.java:17355)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at android.os.Handler.handleCallback(Handler.java:725)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at android.os.Looper.loop(Looper.java:137)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at java.lang.reflect.Method.invoke(Method.java:511)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-14 23:53:26.990: E/AndroidRuntime(1048):     at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 你能把logcat里的错误复制过来加在这里吗?

标签: android xml http-post


【解决方案1】:

您是否请求过互联网权限?如果没有,请将其添加到清单中,如下所示。

<manifest xlmns:android...>
  ...
   <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

编辑:好的,所以堆栈跟踪似乎表明您的 sendXML 字符串为空。可以加一下吗

Log.i("myApp", "sendXML = " + sendXML); 

在 try 块的顶部检查是否是这种情况。它会将其打印到 logcat。

【讨论】:

  • 谢谢,利兹。我已经在清单中请求了 Internet 权限,尽管在文件顶部询问了我的权限。这是一个问题吗?这是logcat:
  • 不,这应该不是问题。我已对我的答案进行了编辑,希望对您有所帮助。
  • sendXML 似乎没有为空。我将使用更新的 logcat 编辑我的原始帖子。感谢您的所有帮助。
  • 当我通过 curl 运行 POST 时,返回 XML 响应确实需要大约 30 秒。会不会是我的应用认为按钮已经崩溃了,因为它需要很长时间?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 2016-02-22
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
相关资源
最近更新 更多