我目前正在开发一个Android应用程序。当将表单的信息插入数据库时,它会发布“很遗憾......已停止”,但在表中添加了一行。但是,我的朋友有相同的代码,它的工作原理。我已经两天寻找解决方案...... 请等待解决方案:)
package com.example.user.presentation;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class insc extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputFullName;
EditText inputLogin;
EditText inputPassword;
EditText inputdate;
EditText inputMala;
EditText inputMed;
EditText inputfixe;
EditText inputmobile;
Spinner sp;
// url to create new product
private static String url_create_product = "http://10.0.2.2/android_connect/create_user.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insc);
sp=(Spinner) findViewById(R.id.spin);
ArrayAdapter<CharSequence> ar = ArrayAdapter.createFromResource(this, R.array.groupesanguin, android.R.layout.simple_list_item_1);
ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp.setAdapter(ar);
// Edit Text
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.editText2);
inputLogin = (EditText) findViewById(R.id.editText3);
inputPassword = (EditText) findViewById(R.id.editText6);
inputdate = (EditText) findViewById(R.id.editText7);
inputMala = (EditText) findViewById(R.id.editText8);
inputMed = (EditText) findViewById(R.id.editText9);
inputfixe = (EditText) findViewById(R.id.editText10);
inputmobile = (EditText) findViewById(R.id.editText11);
// Create button
Button btnCreateProduct = (Button) findViewById(R.id.button2);
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(insc.this);
pDialog.setMessage("Ajout malade..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = inputFullName.getText().toString();
String login = inputLogin.getText().toString();
String password = inputPassword.getText().toString();
String date = inputdate.getText().toString();
String gpesang=sp.getSelectedItem().toString();
String maladies = inputMala.getText().toString();
String medicaments = inputMed.getText().toString();
String fixe = inputfixe.getText().toString();
String mobile = inputmobile.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("login", login));
params.add(new BasicNameValuePair("password",password));
params.add(new BasicNameValuePair("gpesang",gpesang));
params.add(new BasicNameValuePair("date",date));
params.add(new BasicNameValuePair("maladies",maladies));
params.add(new BasicNameValuePair("medicaments",medicaments));
params.add(new BasicNameValuePair("fixe",fixe));
params.add(new BasicNameValuePair("mobile",mobile));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), Connect.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
日志: