Click here to Skip to main content
11,640,445 members (66,010 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android
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 10-Mar-13 21:22pm
Edited 10-Mar-13 21:24pm
Mehdi Gholam282.8K
v2
Comments
vishal.v.patil at 11-Mar-13 3:31am
   
Still got an error!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

thanks friends but i got solution... its not have an internet permission!!!
  Permalink  

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

  Print Answers RSS
0 Mika Wendelius 300
1 CPallini 209
2 OriginalGriff 165
3 Sergey Alexandrovich Kryukov 139
4 Afzaal Ahmad Zeeshan 130
0 Mika Wendelius 200
1 OriginalGriff 145
2 CPallini 115
3 jgakenhe 40
4 sasanka sekhar panda 30


Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 11 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100