我是android开发的新手。我正在尝试使用 android 的 HttpPost 通过 php 脚本将数据插入远程 Mysql 数据库。我已经添加了互联网许可。我收到强制关闭错误。我的代码如下:
package com.regsiteration.regfi;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
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.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Main extends Activity {
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xyz.com/folder/register.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", "demo@ats.in"));
nameValuePairs.add(new BasicNameValuePair("name", "robin hood"));
nameValuePairs.add(new BasicNameValuePair("password", "1234590"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//postData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
请告诉我我错过了什么或做错了什么。提前致谢。这是我的 logcat 跟踪
08-17 20:16:49.530: I/dalvikvm(630): threadid=3: reacting to signal 3
08-17 20:16:49.654: I/dalvikvm(630): Wrote stack traces to '/data/anr/traces.txt'
08-17 20:16:49.779: D/AndroidRuntime(630): Shutting down VM
08-17 20:16:49.779: W/dalvikvm(630): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-17 20:16:49.799: E/AndroidRuntime(630): FATAL EXCEPTION: main
08-17 20:16:49.799: E/AndroidRuntime(630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.regsiteration.regfi/com.regsiteration.regfi.Main}: android.os.NetworkOnMainThreadException
08-17 20:16:49.799: E/AndroidRuntime(630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-17 20:16:49.799: E/AndroidRuntime(630): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-17 20:16:49.799: E/AndroidRuntime(630): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-17 20:16:49.799: E/AndroidRuntime(630): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-17 20:16:49.799: E/AndroidRuntime(630): at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 20:16:49.799: E/AndroidRuntime(630): at android.os.Looper.loop(Looper.java:137)
08-17 20:16:49.799: E/AndroidRuntime(630): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-17 20:16:49.799: E/AndroidRuntime(630): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 20:16:49.799: E/AndroidRuntime(630): at java.lang.reflect.Method.invoke(Method.java:511)
08-17 20:16:49.799: E/AndroidRuntime(630): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-17 20:16:49.799: E/AndroidRuntime(630): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-17 20:16:49.799: E/AndroidRuntime(630): at dalvik.system.NativeStart.main(Native Method)
08-17 20:16:49.799: E/AndroidRuntime(630): Caused by: android.os.NetworkOnMainThreadException
08-17 20:16:49.799: E/AndroidRuntime(630): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-17 20:16:49.799: E/AndroidRuntime(630): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-17 20:16:49.799: E/AndroidRuntime(630): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-17 20:16:49.799: E/AndroidRuntime(630): at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-17 20:16:49.799: E/AndroidRuntime(630): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-17 20:16:49.799: E/AndroidRuntime(630): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-17 20:16:49.799: E/AndroidRuntime(630): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-17 20:16:49.799: E/AndroidRuntime(630): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-17 20:16:49.799: E/AndroidRuntime(630): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-17 20:16:49.799: E/AndroidRuntime(630): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-17 20:16:49.799: E/AndroidRuntime(630): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-17 20:16:49.799: E/AndroidRuntime(630): at com.regsiteration.regfi.Main.postData(Main.java:37)
08-17 20:16:49.799: E/AndroidRuntime(630): at com.regsiteration.regfi.Main.onCreate(Main.java:50)
08-17 20:16:49.799: E/AndroidRuntime(630): at android.app.Activity.performCreate(Activity.java:4465)
08-17 20:16:49.799: E/AndroidRuntime(630): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-17 20:16:49.799: E/AndroidRuntime(630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-17 20:16:49.799: E/AndroidRuntime(630): ... 11 more
08-17 20:16:49.960: I/dalvikvm(630): threadid=3: reacting to signal 3
08-17 20:16:49.970: I/dalvikvm(630): Wrote stack traces to '/data/anr/traces.txt'
08-17 20:16:50.340: I/dalvikvm(630): threadid=3: reacting to signal 3
08-17 20:16:50.350: I/dalvikvm(630): Wrote stack traces to '/data/anr/traces.txt'