2014-07-18 26 views
1

您好我已經開發了一個應用程序來發布數據從android到網站。在從Android應用程序發佈數據到網站的問題

我有問題android.os.NetworkOnMainThreadException然後我用的AsyncTask過度來到這個問題,因爲在這裏我以前的線程(How To Post Data From An Android App To a Website

現在

建議當我運行的應用程序,我得到消息,不幸的是應用程序有停止了關閉應用的 應用。現在問題在哪裏?

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(); 
     } 

}} 

XML

<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腳本是

<?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; 
?> 

LOGCAT

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 

回答

3

發送方法需要是MainActivity的方法。 你的代碼不是。

+2

更具體地說,它不是Activity類的一個方法,而是內部AsyncTask類的方法,因爲關閉內部類的括號緊隨其後,而不是之前。移動支架應該修復它。 –

0

問題是你已經爲佈局中的按鈕定義了onClick屬性。但是Activity類沒有實現。

相關問題