2013-03-14 132 views
0

您好精彩的SO!Android - 使用HTTPPOST发送和接收XML

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

定义一些变量...

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

httppost是我发布到的URL。

点是一个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”当然有正确的网址发布到:

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) 
+1

你可以从logcat复制错误并将其添加到此处? – leedsunited92 2013-03-14 18:50:54

回答

0

您是否要求上网权限?如果没有,请按如下所示将其添加到清单。

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

编辑:确定所以堆栈跟踪似乎建议您的sendXML字符串为空。你可以添加

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

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

+0

谢谢,利兹。我已经要求清单中的互联网许可,尽管我在文件顶部询问了我的内容。这是一个问题吗?这里是logcat: – user2171109 2013-03-14 19:04:43

+0

对不起,logcat包含在我上面编辑的文章中。 – user2171109 2013-03-14 19:06:22

+0

不,这不应该是一个问题。我已经添加了一个编辑到我的答案,希望有所帮助。 – leedsunited92 2013-03-14 19:10:13