Click here to Skip to main content
15,907,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
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) {
		// TODO Auto-generated method stub
		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
Posted
Updated 10-Mar-13 21:24pm
v2
Comments
vishal.v.patil 11-Mar-13 3:31am    
Still got an error!!

This is because of later version of Android does not allow accessing to the network in main thread: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html[^].

To solve this problem, use AsyncTask or Thread to perform your task. Here's an example of using AsyncTask: http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception[^]
 
Share this answer
 
v2
thanks friends but i got solution... its not have an internet permission!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900