你好精神的SO!
我知道这个问题及其版本已经在SO上多次提出,我已经阅读了我可以在这个问题上找到的每个主题。我已经尝试了我能找到的每种解决方案组合,但不知怎的,我仍然遇到问题。我希望你不要介意我创建自己的问题,这样我就可以发布我的特定代码。我在Android应用程序上完全是绿色的,并且具有一些基本的Java背景。
定义一些变量......
sendXML是一个字符串,是我的XML代码。
httppost是我要发布的网址。
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)
答案 0 :(得分:0)
您是否要求上网权限?如果没有,请按如下方式将其添加到清单中。
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
编辑:好的,所以堆栈跟踪似乎表明你的sendXML字符串为空。你能加
吗?Log.i("myApp", "sendXML = " + sendXML);
在try块的顶部检查是否是这种情况。它会将它打印到logcat。