我插入數據到一個mysql數據庫與我的android應用程序中的功能,但它每次我點擊按鈕執行它崩潰。這些是我得到的錯誤:插入數據從Android到PHP文件,然後到Mysql錯誤
10-08 16:16:05.649:E/AndroidRuntime(19038):致命異常:主 10-08 16:16:05.649:E/AndroidRuntime(19038):android。 os.NetworkOnMainThreadException 10-08 16:16:05.649:E/AndroidRuntime(19038):at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125) 10-08 16:16:05.649:E/AndroidRuntime (19038):at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 10-08 16:16:05.649:E/AndroidRuntime(19038):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236 ) 10-08 16:16:05.649:E/AndroidRuntime(19038):at java.net.InetAddress.getAllByName(InetAddress.java:214) 10-08 16:16:05.649:E/AndroidRuntime(19038):在org.apache.h ttp.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 10-08 16:16:05.649:E/AndroidRuntime(19038):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry。 java:164) 10-08 16:16:05.649:E/AndroidRuntime(19038):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 10-08 16:16: 05.649:E/AndroidRuntime(19038):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 10-08 16:16:05.649:E/AndroidRuntime(19038):at org。 apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670) 10-08 16:16:05.649:E/AndroidRuntime(19038):at org.apache.http.impl.client.AbstractHttpClient.execute( AbstractHttpClient.java:509) 10-08 16:16:05.649:E/AndroidRuntim e(19038):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 10-08 16:16:05.649:E/AndroidRuntime(19038):at com.example.gloc。 MainActivity.insert(MainActivity.java:360) 10-08 16:16:05.649:E/AndroidRuntime(19038):at com.example.gloc.MainActivity $ 3.onClick(MainActivity.java:217) 10-08 16 :16:05.649:E/AndroidRuntime(19038):at android.view.View.performClick(View.java:4354) 10-08 16:16:05.649:E/AndroidRuntime(19038):at android.view.View $ PerformClick.run(View.java:17961) 10-08 16:16:05.649:E/AndroidRuntime(19038):at android.os.Handler.handleCallback(Handler.java:725) 10-08 16:16 :05.649:E/AndroidRuntime(19038):at android.os.Handler.dispatchMessage(Handler.java:92) 10-08 16:16:05.649:E/AndroidRuntime(19038):at android.os.Looper .loop(Looper.java:137) 10-08 16:16:05.649:E/AndroidRuntime(19038):at android.app.ActivityThread.main(ActivityThread.java:5328) 10-08 16:16:05.649 :E/AndroidRuntime(19038):at java.lang.reflect.Method.invokeNative(Native Method) 10-08 16:16:05.649:E/AndroidRuntime(19038):at java.lang.reflect.Method.invoke Method.java:511) 10-08 16:16:05.649:E/AndroidRuntime(19038):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102) 10-08 16: 16:05.649:E/AndroidRuntime(19038):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 10-08 16:16:05.649:E/AndroidRuntime(19038):at dalvik。 system.NativeStart.main(Native Method)
這是我的代碼
public void insert(){
String input_data= "http://www.wolfdoginfo.net/app/dataapp.php"; //URL website anda dengan file insert.php yang telah dibuat
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(input_data);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
final EditText reguser = (EditText) findViewById(R.id.usernamecomplete);
final EditText regpass = (EditText) findViewById(R.id.passwordcomplete);
final EditText regemail = (EditText) findViewById(R.id.email);
String password = regpass.getText().toString();
password = md5(password);
param.add(new BasicNameValuePair("username", reguser.getText().toString()));
param.add(new BasicNameValuePair("password", password));
param.add(new BasicNameValuePair("email", regemail.getText().toString()));
try {
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpRespose = httpClient.execute(httpPost);
HttpEntity httpEntity = httpRespose.getEntity();
InputStream in = httpEntity.getContent();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
String isi= "";
String baris= "";
while((baris = read.readLine())!=null){
isi+= baris;
}
//Jika isi tidak sama dengan "null " maka akan tampil Toast "Berhasil" sebaliknya akan tampil "Gagal"
if(!isi.equals("null")){
Toast.makeText(this, "Berhasil", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Gagal", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
好,我把它放在一個線程下,但仍TI保存犯規數據庫。 – user2030485
我回答了最初的問題。你的PHP代碼可能是罪魁禍首,如果你的插入不工作。 – SoundsDangerous
即使在單獨的線程上運行代碼,你是否也得到相同的異常? – InPursuit