您好我开发了一个应用程序,用于将数据从Android发布到网站。
我遇到问题android.os.NetworkOnMainThreadException然后我使用AsyncTask来解决这个问题,就像我之前的帖子(How To Post Data From An Android App To a Website)中所建议的那样
现在,当我运行应用程序时,我收到了消息,不幸的是app已经停止了 应用关闭。问题现在在哪里?
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 SCript是
<?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
答案 0 :(得分:3)
send方法需要是MainActivity的方法。 你的代码不是。
答案 1 :(得分:0)
问题是您已在布局中为Button定义了onClick属性。但是Activity类中没有实现。