Java 以下是Android HTTP示例/教程-为网络活动实现异步任务的问题

Java 以下是Android HTTP示例/教程-为网络活动实现异步任务的问题,java,android,http,post,Java,Android,Http,Post,我正试图遵循一个关于发布HTTP数据的教程,但是由于IO线程上的网络活动,它在主线程上强制关闭,因此我尝试按照其他一些有用的so用户的建议实现AsyncTask-但是我已经破坏了代码,整个enchilada强制关闭了我 : 提前谢谢 努布尼加 爪哇: XML: PHP: 日志: 03-27 16:30:00.775: D/dalvikvm(14566): GC_FOR_ALLOC freed 88K, 3% free 7355K/7568K, paused 36ms, total 36m

我正试图遵循一个关于发布HTTP数据的教程,但是由于IO线程上的网络活动,它在主线程上强制关闭,因此我尝试按照其他一些有用的so用户的建议实现AsyncTask-但是我已经破坏了代码,整个enchilada强制关闭了我

:

提前谢谢

努布尼加

爪哇:

XML:

PHP:

日志:

    03-27 16:30:00.775: D/dalvikvm(14566): GC_FOR_ALLOC freed 88K, 3% free 7355K/7568K, paused 36ms, total 36ms
03-27 16:30:00.785: I/dalvikvm-heap(14566): Grow heap (frag case) to 10.818MB for 3686416-byte allocation
03-27 16:30:00.825: D/dalvikvm(14566): GC_FOR_ALLOC freed 1K, 2% free 10953K/11172K, paused 35ms, total 35ms
03-27 16:30:00.845: D/dalvikvm(14566): GC_CONCURRENT freed <1K, 2% free 10953K/11172K, paused 3ms+3ms, total 29ms
03-27 16:30:01.095: D/dalvikvm(14566): GC_FOR_ALLOC freed <1K, 2% free 10953K/11172K, paused 16ms, total 16ms
03-27 16:30:01.105: I/dalvikvm-heap(14566): Grow heap (frag case) to 17.043MB for 6529744-byte allocation
03-27 16:30:01.125: D/dalvikvm(14566): GC_FOR_ALLOC freed 0K, 2% free 17330K/17552K, paused 16ms, total 16ms
03-27 16:30:01.145: D/dalvikvm(14566): GC_CONCURRENT freed 0K, 2% free 17330K/17552K, paused 3ms+3ms, total 23ms
03-27 16:30:01.275: D/libEGL(14566): loaded /system/lib/egl/libEGL_tegra.so
03-27 16:30:01.285: D/libEGL(14566): loaded /system/lib/egl/libGLESv1_CM_tegra.so
03-27 16:30:01.295: D/libEGL(14566): loaded /system/lib/egl/libGLESv2_tegra.so
03-27 16:30:01.325: D/OpenGLRenderer(14566): Enabling debug mode 0
03-27 16:30:03.325: D/AndroidRuntime(14566): Shutting down VM
03-27 16:30:03.325: W/dalvikvm(14566): threadid=1: thread exiting with uncaught exception (group=0x41cd2930)
03-27 16:30:03.325: E/AndroidRuntime(14566): FATAL EXCEPTION: main
03-27 16:30:03.325: E/AndroidRuntime(14566): java.lang.Error: Unresolved compilation problems: 
03-27 16:30:03.325: E/AndroidRuntime(14566):    Syntax error on token ".", ; expected
03-27 16:30:03.325: E/AndroidRuntime(14566):    Syntax error, insert ";" to complete FieldDeclaration
03-27 16:30:03.325: E/AndroidRuntime(14566):    Syntax error, insert "}" to complete ClassBody
03-27 16:30:03.325: E/AndroidRuntime(14566):    at com.nfc.linkingmanager.DeviceConfig.<init>(DeviceConfig.java:48)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at java.lang.Class.newInstanceImpl(Native Method)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at java.lang.Class.newInstance(Class.java:1319)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.os.Looper.loop(Looper.java:137)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.ActivityThread.main(ActivityThread.java:5041)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at java.lang.reflect.Method.invokeNative(Native Method)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at java.lang.reflect.Method.invoke(Method.java:511)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at dalvik.system.NativeStart.main(Native Method)
NetworkOnMainThreadException

您将获得异常,因为您在主线程上执行的网络操作不允许android版本>=3.0

使用AsyncTask执行网络操作

使用doInBackground方法进行网络操作


阅读android文档。

您是否使用LogCat生成日志?您正在尝试在UI线程上执行网络操作,如果您使用的是android 3.x或更高版本,则会出现异常。@NoobNinja根据答案更新您的问题是不好的。@NoobNinja您犯了一个大错误。学习SO的规则。不是这样的,如果你找到了答案,然后用不同的问题编辑完整的问题。@Pragnani我们现在应该采取行动。我们不应该吗?我在上面实现了它。。。但它仍在崩溃,我需要帮助清理项目一点-但使用eclipse的建议完全不正确-它要求我把;在一些看起来很奇怪的地方,你用了不需要的大括号和分号。在eclipse中签入代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:text="Message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        /> 

    <EditText
        android:id="@+id/msgTextField"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:text="Send"
        android:id="@+id/sendButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="send"
        /> 

</LinearLayout>
<?php
// get the "message" variable from the post request
// this is the data coming from the Android app
$message=$_POST["message"]; 
// specify the file where we will save the contents of the variable message
$filename="androidmessages.html";
// write (append) the data to the file
file_put_contents($filename,$message."<br />",FILE_APPEND);
// load the contents of the file to a variable
$androidmessages=file_get_contents($filename);
// display the contents of the variable (which has the contents of the file)
echo $androidmessages;
?>
    03-27 16:30:00.775: D/dalvikvm(14566): GC_FOR_ALLOC freed 88K, 3% free 7355K/7568K, paused 36ms, total 36ms
03-27 16:30:00.785: I/dalvikvm-heap(14566): Grow heap (frag case) to 10.818MB for 3686416-byte allocation
03-27 16:30:00.825: D/dalvikvm(14566): GC_FOR_ALLOC freed 1K, 2% free 10953K/11172K, paused 35ms, total 35ms
03-27 16:30:00.845: D/dalvikvm(14566): GC_CONCURRENT freed <1K, 2% free 10953K/11172K, paused 3ms+3ms, total 29ms
03-27 16:30:01.095: D/dalvikvm(14566): GC_FOR_ALLOC freed <1K, 2% free 10953K/11172K, paused 16ms, total 16ms
03-27 16:30:01.105: I/dalvikvm-heap(14566): Grow heap (frag case) to 17.043MB for 6529744-byte allocation
03-27 16:30:01.125: D/dalvikvm(14566): GC_FOR_ALLOC freed 0K, 2% free 17330K/17552K, paused 16ms, total 16ms
03-27 16:30:01.145: D/dalvikvm(14566): GC_CONCURRENT freed 0K, 2% free 17330K/17552K, paused 3ms+3ms, total 23ms
03-27 16:30:01.275: D/libEGL(14566): loaded /system/lib/egl/libEGL_tegra.so
03-27 16:30:01.285: D/libEGL(14566): loaded /system/lib/egl/libGLESv1_CM_tegra.so
03-27 16:30:01.295: D/libEGL(14566): loaded /system/lib/egl/libGLESv2_tegra.so
03-27 16:30:01.325: D/OpenGLRenderer(14566): Enabling debug mode 0
03-27 16:30:03.325: D/AndroidRuntime(14566): Shutting down VM
03-27 16:30:03.325: W/dalvikvm(14566): threadid=1: thread exiting with uncaught exception (group=0x41cd2930)
03-27 16:30:03.325: E/AndroidRuntime(14566): FATAL EXCEPTION: main
03-27 16:30:03.325: E/AndroidRuntime(14566): java.lang.Error: Unresolved compilation problems: 
03-27 16:30:03.325: E/AndroidRuntime(14566):    Syntax error on token ".", ; expected
03-27 16:30:03.325: E/AndroidRuntime(14566):    Syntax error, insert ";" to complete FieldDeclaration
03-27 16:30:03.325: E/AndroidRuntime(14566):    Syntax error, insert "}" to complete ClassBody
03-27 16:30:03.325: E/AndroidRuntime(14566):    at com.nfc.linkingmanager.DeviceConfig.<init>(DeviceConfig.java:48)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at java.lang.Class.newInstanceImpl(Native Method)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at java.lang.Class.newInstance(Class.java:1319)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.os.Looper.loop(Looper.java:137)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at android.app.ActivityThread.main(ActivityThread.java:5041)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at java.lang.reflect.Method.invokeNative(Native Method)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at java.lang.reflect.Method.invoke(Method.java:511)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-27 16:30:03.325: E/AndroidRuntime(14566):    at dalvik.system.NativeStart.main(Native Method)