-1

Inserting 6 columns from android to PHP MySql showing these 3 errors Implementing App/code using Fragment and this code is done on One of the fragment file. Added Internet permission also in Manifest

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();

    }
});

Insert Function in the same file

    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());
    }
}

New to Inserting in Data in Mysql from Android Thankyou

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
Learner
  • 33
  • 7

4 Answers4

1

Android does not allow any network operation on UI thread, you need to create separate thread to do so. You can use asynctask for this, like as follows:

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

    @Override
    protected Void doInBackground(Void... params) {
        insert();
    }
}
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
Mohd Asif Ahmed
  • 1,880
  • 2
  • 15
  • 29
  • after implementing this ..I am having one problem which is now it's giving error -> java.lang.RuntimeException: **Can't create handler inside thread that has not called Looper.prepare()** – Learner Jun 24 '17 at 05:45
0

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

Refer

1. AsyncTask Documentation

2. AsyncTask Example

0

Your stacktrace clearly states that you are calling network API from the UI thread. You should use a background thread or AsyncTask instead.

Darish
  • 11,032
  • 5
  • 50
  • 70
0

As for the NetworkOnMainThreadException you need to move the insert call on a background thread which is due to

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

, for that you need to use a 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                       
        }
    });

and also if theres any update needed to run on the main thread on insert method call, runOnUiThread like Toast, alerts. etc

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

if you are using a fragment call

 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.

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49