package com.example.bms;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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 android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Add_Customer extends Activity{
EditText customerName,addr1,addr2,person1,mob,pos,note;
Button Save,cancel;
List<namevaluepair> nameValuePairs;
InputStream is;
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_customer);
customerName=(EditText)findViewById(R.id.CustomerName);
addr1=(EditText)findViewById(R.id.addr1);
Save=(Button)findViewById(R.id.save);
cancel=(Button)findViewById(R.id.cancel);
addr2=(EditText)findViewById(R.id.addr2);
person1=(EditText)findViewById(R.id.Person);
mob=(EditText)findViewById(R.id.Mobile);
pos=(EditText)findViewById(R.id.Position);
note=(EditText)findViewById(R.id.tNote);
Save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://123.12.11.1/insertCustomer.php");
nameValuePairs = new ArrayList<namevaluepair>(3);
nameValuePairs.add(new BasicNameValuePair("cName", customerName.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("addr1", addr1.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("addr2", addr2.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("person1", person1.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("mob", mob.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("pos", pos.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("note", note.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("date", dateFormat.format(cal.getTime())));
UrlEncodedFormEntity URLEntity = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(URLEntity );
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
int status = response.getStatusLine().getStatusCode();
if(status==200)
Toast.makeText(Add_Customer.this,"New Customer Information added,Successfully !", Toast.LENGTH_LONG).show();
else Toast.makeText(Add_Customer.this,"Customer Information Not Saved, Error in http Connection: ", Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(Add_Customer.this,"Error in http Connection: "+e.toString(), Toast.LENGTH_LONG).show();
}
}
});
}
}
this gots the error of network on main thread exception.. how i resolve!??
Thnnks regards.
Vishal