从Android应用程序向网站发布数据时出现问题

从Android应用程序向网站发布数据时出现问题,android,Android,嗨,我开发了一个应用程序,可以将安卓系统的数据发布到网站上 我遇到了android.os.NetworkOnMainThreadException问题,然后我使用AsyncTask来解决这个问题,正如我在上一个线程()中建议的那样 现在,当我运行应用程序时,我收到一条消息,不幸的是,应用程序已停止运行 应用程序关闭。现在的问题在哪里 package com.latlongapp; import java.io.IOException; import java.util.ArrayList; i

嗨,我开发了一个应用程序,可以将安卓系统的数据发布到网站上

我遇到了android.os.NetworkOnMainThreadException问题,然后我使用AsyncTask来解决这个问题,正如我在上一个线程()中建议的那样

现在,当我运行应用程序时,我收到一条消息,不幸的是,应用程序已停止运行 应用程序关闭。现在的问题在哪里

package com.latlongapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

    Button sendButton;

    EditText msgTextField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         // make message text field object
        msgTextField = (EditText) findViewById(R.id.msgTextField);
        // make send button object
        sendButton = (Button) findViewById(R.id.sendButton);
    }

    class SendTask extends AsyncTask<String, Void, String> {
        @Override
            protected String doInBackground(String... params) {
                  // get the message from the message text box
                String msg = msgTextField.getText().toString();  

                // make sure the fields are not empty
                if (msg.length()>0)
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://http://tayyab001.base.pk/kami.php");
                 try {
                   List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                   nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                   nameValuePairs.add(new BasicNameValuePair("message", msg));
                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                   httpclient.execute(httppost);
                   msgTextField.setText(""); // clear text box
                 } catch (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                 }

                }
                else
                {
                    // display message if text fields are empty
                    Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
                }
             return null;
        }

     public void send(View v)
        {
         new SendTask().execute();
        }

}}

send方法必须是MainActivity的方法。
您的代码不是。问题是您已经为布局中的按钮定义了onClick属性。但是Activity类中没有实现。

更具体地说,它不是Activity类的方法,而是内部AsyncTask类的方法,因为关闭内部类的括号位于它之后而不是之前。移动支架应能将其固定。
<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"
    /> 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />


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



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.latlongapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<?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;
?>
07-19 00:37:11.148: W/dalvikvm(23394): threadid=1: thread exiting with uncaught exception (group=0x410ac9a8)
07-19 00:37:11.168: E/AndroidRuntime(23394): FATAL EXCEPTION: main
07-19 00:37:11.168: E/AndroidRuntime(23394): java.lang.IllegalStateException: Could not find a method send(View) in the activity class com.latlongapp.MainActivity for onClick handler on view class android.widget.Button with id 'sendButton'
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.view.View$1.onClick(View.java:3594)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.view.View.performClick(View.java:4212)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.view.View$PerformClick.run(View.java:17476)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.os.Handler.handleCallback(Handler.java:800)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.os.Handler.dispatchMessage(Handler.java:100)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.os.Looper.loop(Looper.java:194)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.app.ActivityThread.main(ActivityThread.java:5371)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at java.lang.reflect.Method.invokeNative(Native Method)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at java.lang.reflect.Method.invoke(Method.java:525)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at dalvik.system.NativeStart.main(Native Method)
07-19 00:37:11.168: E/AndroidRuntime(23394): Caused by: java.lang.NoSuchMethodException: send [class android.view.View]
07-19 00:37:11.168: E/AndroidRuntime(23394):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at java.lang.Class.getMethod(Class.java:915)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.view.View$1.onClick(View.java:3587)
07-19 00:37:11.168: E/AndroidRuntime(23394):    ... 11 more