L'insertion de 6 colonnes d'android à PHP MySql montrant ces 3 erreurs L'application App/code en utilisant Fragment et ce code est fait sur Un du fichier fragment. Ajouté autorisations Internet aussi dans ManifestErreur: Lors de l'insertion de données dans MySQL à partir d'Android
Log Cat
06-23 00:52:53.830: E/Fail 1(1512):android.os.NetworkOnMainThreadException
06-23 00:52:53.830: E/Fail 2(1512):java.lang.NullPointerException:lock==null
06-23 00:52:53.830: E/Fail 3(1512):java.lang.NullPointerException
Android code
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addque = question.getText().toString();
addc1 = choice1.getText().toString();
addc2 = choice2.getText().toString();
addc3 = choice3.getText().toString();
addanswer = answer.getText().toString();
addexplan = Explanation.getText().toString();
insert();
//Toast.makeText(getActivity(), "Question posted", Toast.LENGTH_SHORT).show();
}
});
Insérer une fonction dans le même fichier
public void insert() {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("addque", addque));
nameValuePairs.add(new BasicNameValuePair("addc1", addc1));
nameValuePairs.add(new BasicNameValuePair("addc2", addc2));
nameValuePairs.add(new BasicNameValuePair("addc3", addc3));
nameValuePairs.add(new BasicNameValuePair("addanswer", addanswer));
nameValuePairs.add(new BasicNameValuePair("addexplan", addexplan));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://localhost/insert/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
} catch (Exception e) {
Log.e("Fail 1", e.toString());
//Toast.makeText(getApplicationContext(), "Invalid IP
Address ",Toast.LENGTH_LONG).show();
}
try {
BufferedReader reader = new BufferedReader
(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
} catch (Exception e) {
Log.e("Fail 2", e.toString());
}
try {
JSONObject json_data = new JSONObject(result);
code = (json_data.getInt("code"));
if (code == 1) {
Toast.makeText(getActivity(), "Inserted Successfully",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.e("Fail 3", e.toString());
}
}
Nouveau sur Insertion dans les données Mysql de Android Remerciements
déplacer votre insert() en fil de fond, au lieu de l'appeler dans le thread principal –