2017-06-23 1 views
-1

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

+0

déplacer votre insert() en fil de fond, au lieu de l'appeler dans le thread principal –

Répondre

1

Android n'autorise aucune opération réseau sur le thread de l'interface utilisateur. Pour cela, vous devez créer un thread séparé. Vous pouvez utiliser AsyncTask pour cela, comme la façon suivante:

class MyAsyncTask extends AsyncTask<Void,Void,Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 
     insert(); 
    } 
} 
+0

après la mise en œuvre de cette ..Je suis ayant un problème qui est maintenant il donne l'erreur -> java.lang.RuntimeException: ** Impossible de créer le gestionnaire dans le thread qui n'a pas appelé Looper.prepare() ** – LazyLearner

0

Votre stacktrace indique clairement que vous appelez API réseau à partir du thread d'interface utilisateur. Vous devez utiliser un thread d'arrière-plan ou AsyncTask à la place.

0

Quant au NetworkOnMainThreadException vous devez déplacer l'appel d'insertion sur un fil d'arrière-plan qui est dû à

You can't call network operation on main thread , you have to create background thread which execute your operation.

, pour que vous devez utiliser un AsyncTask

AsyncTask.execute(new Runnable() { 
     @Override 
     public void run() { 
      insert(); // call insert method in this block 
         // which is background thread, 
         // prevents network call to run on MainThread      
     } 
    }); 

et aussi s'il y a une mise à jour nécessaire pour faire fonctionner sur le thread principal sur appel de la méthode d'insertion, runOnUiThread comme Toast, des alertes. etc

runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      // Toast, Alerts or any operation that needs 
      // to interact on UI, call here 
     } 
    }); 

si vous utilisez un appel fragment

getActivity().runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      // Toast, Alerts or any operation that needs 
      // to interact on UI, call here 
     } 
    }); 

PS: You need to have a clear understanding of these topics before implementing, So you have to go through the developer docs.