我无法将数据发送到我的数据库。我在另一个类中使用相同类型的代码并且工作正常,但不知何故这不起作用。
这是具有 HttpResponse 的类。没有错误,但是当代码到达
HttpResponse response = httpclient.execute(httppost);
它进入捕获并返回“未添加 Klant”)
public void StoreKlantInDatabase()
{
EditText ETklantnaam = (EditText) dialog.findViewById(R.id.addKlantNaam);
EditText ETlocatie = (EditText) dialog.findViewById(R.id.addKlantLocatie);
EditText ETfabriek = (EditText) dialog.findViewById(R.id.addFabrieken);
EditText ETmachine = (EditText) dialog.findViewById(R.id.addMachines);
String klantnaam = ETklantnaam.getText().toString();
String locatie = ETlocatie.getText().toString();
String fabriek = ETfabriek.getText().toString();
String machine= ETmachine.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlInsertKlant);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("klantnaam", klantnaam));
nameValuePairs.add(new BasicNameValuePair("locatie", locatie));
nameValuePairs.add(new BasicNameValuePair("fabriek", fabriek));
nameValuePairs.add(new BasicNameValuePair("machine", machine));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
@SuppressWarnings("unused")
HttpResponse response = httpclient.execute(httppost);
Toast.makeText(getApplicationContext(), "Klant succesvol toegevoegd", Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Klant NIET toegevoegd", Toast.LENGTH_LONG).show();
}
}
此方法从对话框中调用,如下所示(在最后一个 OnClick 方法中):
btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
dialog = new Dialog(context);
dialog.setContentView(R.layout.addklant_layout);
dialog.setTitle("Klant toevoegen");
Button BTNannuleren = (Button) dialog.findViewById(R.id.dialogAnnuleren);
// if button is clicked, close the custom dialog
BTNannuleren.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button BTNtoevoegen = (Button) dialog.findViewById(R.id.dialogToevoegen);
// if button is clicked, close the custom dialog
BTNtoevoegen.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StoreKlantInDatabase();
dialog.dismiss();
}
});
dialog.show();
}
});
我试图在对话框的 onClick 方法中完成这一切,但这没有任何区别。
Ofcouse对话框是在代码顶部启动的,例如:
Dialog dialog;
有谁知道我哪里出错了?我有点失落。
编辑:这发生在捕获中:
09-30 09:36:52.452: W/System.err(14665): android.os.NetworkOnMainThreadException
09-30 09:36:52.482: W/System.err(14665): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1186)
09-30 09:36:52.482: W/System.err(14665): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-30 09:36:52.482: W/System.err(14665): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-30 09:36:52.482: W/System.err(14665): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-30 09:36:52.482: W/System.err(14665): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-30 09:36:52.482: W/System.err(14665): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-30 09:36:52.482: W/System.err(14665): at java.net.Socket.connect(Socket.java:872)
09-30 09:36:52.482: W/System.err(14665): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
09-30 09:36:52.482: W/System.err(14665): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
09-30 09:36:52.482: W/System.err(14665): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-30 09:36:52.482: W/System.err(14665): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-30 09:36:52.482: W/System.err(14665): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-30 09:36:52.482: W/System.err(14665): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:591)
09-30 09:36:52.482: W/System.err(14665): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:510)
09-30 09:36:52.482: W/System.err(14665): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:488)
09-30 09:36:52.482: W/System.err(14665): at com.example.emmtec.FirstScreen.StoreKlantInDatabase(FirstScreen.java:272)
09-30 09:36:52.482: W/System.err(14665): at com.example.emmtec.FirstScreen$10$2.onClick(FirstScreen.java:236)
09-30 09:36:52.482: W/System.err(14665): at android.view.View.performClick(View.java:3538)
09-30 09:36:52.492: W/System.err(14665): at android.view.View$PerformClick.run(View.java:14330)
09-30 09:36:52.492: W/System.err(14665): at android.os.Handler.handleCallback(Handler.java:608)
09-30 09:36:52.492: W/System.err(14665): at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 09:36:52.492: W/System.err(14665): at android.os.Looper.loop(Looper.java:156)
09-30 09:36:52.492: W/System.err(14665): at android.app.ActivityThread.main(ActivityThread.java:4977)
09-30 09:36:52.492: W/System.err(14665): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 09:36:52.492: W/System.err(14665): at java.lang.reflect.Method.invoke(Method.java:511)
09-30 09:36:52.492: W/System.err(14665): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-30 09:36:52.492: W/System.err(14665): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-30 09:36:52.492: W/System.err(14665): at dalvik.system.NativeStart.main(Native Method)
提前致谢!