This question already has an answer here:
- How do I fix 'android.os.NetworkOnMainThreadException'? 54 answers
On the click event add data to MySQL
database through php
on the localhost. Its showing following logcat error on the click event. Please help me to solve this problem.
08-11 07:31:06.416: D/gralloc_goldfish(971): Emulator without GPU emulation detected.
08-11 07:31:53.377: D/Inside Click(971): aaaaaaa
08-11 07:31:53.837: D/inid doinbackground(971): aaaaaaa
08-11 07:31:53.927: W/dalvikvm(971): threadid=11: thread exiting with uncaught exception (group=0x409961f8)
08-11 07:31:54.047: E/AndroidRuntime(971): FATAL EXCEPTION: AsyncTask #1
08-11 07:31:54.047: E/AndroidRuntime(971): java.lang.RuntimeException: An error occured while executing doInBackground()
08-11 07:31:54.047: E/AndroidRuntime(971): at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-11 07:31:54.047: E/AndroidRuntime(971): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-11 07:31:54.047: E/AndroidRuntime(971): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-11 07:31:54.047: E/AndroidRuntime(971): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-11 07:31:54.047: E/AndroidRuntime(971): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-11 07:31:54.047: E/AndroidRuntime(971): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-11 07:31:54.047: E/AndroidRuntime(971): at java.lang.Thread.run(Thread.java:856)
08-11 07:31:54.047: E/AndroidRuntime(971): Caused by: java.lang.NullPointerException
08-11 07:31:54.047: E/AndroidRuntime(971): at com.example.connectiondemo.MainActivity$CreateBooking.doInBackground(MainActivity.java:1)
08-11 07:31:54.047: E/AndroidRuntime(971): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-11 07:31:54.047: E/AndroidRuntime(971): ... 5 more
08-11 07:31:58.986: I/Process(971): Sending signal. PID: 971 SIG: 9
My Activity
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String url="http://10.0.2.2/styletapphp/create_booking.php";
private static final String TAG_SUCCESS="Success";
JSONParser jsonParser;
JSONObject json;
TextView t1,t2,t3,t4,t5,t6,t7,t8,t9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView) findViewById(R.id.t1);
t2=(TextView) findViewById(R.id.t2);
t3=(TextView) findViewById(R.id.t3);
t4=(TextView) findViewById(R.id.t4);
t5=(TextView) findViewById(R.id.t5);
t6=(TextView) findViewById(R.id.t6);
t7=(TextView) findViewById(R.id.t7);
t8=(TextView) findViewById(R.id.t8);
//t9=(TextView) findViewById(R.id.t8);
t1.setText("Date");
t2.setText("Stylist");
t3.setText("9to10");
t4.setText("10to11");
t5.setText("11to12");
t6.setText("12to1");
t7.setText("2to3");
t8.setText("3to4");
Button butt1= (Button) findViewById(R.id.b1);
butt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Inside Click","aaaaaaa");
//new CreateBooking().execute();
new CreateBooking().execute("Date","Stylist", "9to10", "10to11", "11to12", "12to1","2to3", "3to4");
}
});
}
class CreateBooking extends AsyncTask<String,Integer,String>{
@Override
protected String doInBackground(String... params) {
Log.d("inid doinbackground","aaaaaaa");
List<NameValuePair> namevaluepair = new ArrayList<NameValuePair>();
namevaluepair.add(new BasicNameValuePair("bookingdate", params[0]));
namevaluepair.add(new BasicNameValuePair("stylist", params[1]));
namevaluepair.add(new BasicNameValuePair("status9to10", params[2]));
namevaluepair.add(new BasicNameValuePair("status10to11", params[3]));
namevaluepair.add(new BasicNameValuePair("status11to12", params[4]));
namevaluepair.add(new BasicNameValuePair("status12to1", params[5]));
namevaluepair.add(new BasicNameValuePair("status2to3", params[6]));
namevaluepair.add(new BasicNameValuePair("status3to4", params[7]));
/*List<NameValuePair>namevaluepair=new ArrayList<NameValuePair>();
namevaluepair.add(new BasicNameValuePair("bookingdate",t1.getText().toString()));
namevaluepair.add(new BasicNameValuePair("stylist",t2.getText().toString()));
namevaluepair.add(new BasicNameValuePair("status9to10",t3.getText().toString()));
namevaluepair.add(new BasicNameValuePair("status10to11",t4.getText().toString()));
namevaluepair.add(new BasicNameValuePair("status11to12",t5.getText().toString()));
namevaluepair.add(new BasicNameValuePair("status12to1",t6.getText().toString()));
namevaluepair.add(new BasicNameValuePair("status2to3",t7.getText().toString()));
namevaluepair.add(new BasicNameValuePair("status3to4",t8.getText().toString()));*/
json=jsonParser.makehttpRequest(url,"POST",namevaluepair);
Log.d("Create booking response",json.toString());
// check for success tag
try{int success=json.getInt(TAG_SUCCESS);
publishProgress(success);
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
/*protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
if (integer == null)
return;
if (integer.intValue() == 1) {
Toast.makeText(getApplicationContext(), "booking successfull", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "booking not successfull", Toast.LENGTH_LONG).show();
}
}
protected void onPostExecute(int i)
{
super.onPostExecute(i);
if (i==1){
// successfully created product
Toast.makeText(getApplicationContext(), "booking successfull", Toast.LENGTH_LONG).show();
}else{
// failed to create product
Toast.makeText(getApplicationContext(), "booking not successfull", Toast.LENGTH_LONG).show();
}
// if condition and toast message
} */
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (values[0] == null)
return;
if (values[0].intValue() == 1) {
Toast.makeText(getApplicationContext(), "booking successfull", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "booking not successfull", Toast.LENGTH_LONG).show();
}
}