0

你好!

我知道这个问题和它的版本已经在 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() 的按钮时,我的整个应用程序都会崩溃。任何帮助将不胜感激!

日志如下:

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)
4

1 回答 1

0

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

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

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

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

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

于 2013-03-14T18:55:45.913 回答