Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
This is my code for insert data into the Mysql Database with php and android i am success fully inserted data into database but its can't display the Toast massage of "Inserted Successfully" what is the proble i made in my coding please any one help me
here is my PHP file for insert data..
insert.php
............
header('Content-type=application/json; charset=utf-8');
$con=mysql_connect('127.0.0.1','root','') or die("Connection Failed");
$db=mysql_select_db("Android_Database",$con) or die("Database not found!");

$no=$_REQUEST['no'];
$name=$_REQUEST['name'];

$flag['code']=0;

if($query=mysql_query("insert into info values('$no','$name')",$con))
{
$flag['code']=1;
}
print(json_encode($flag));
mysql_close($con);
?>
............

And this is my Android Activity file
MainActivity.java
................................
public class MainActivity extends ActionBarActivity
{
Button e_btn;
EditText e_no,e_name;
String no,name;
InputStream is;
String result=null;
String line=null;
int code;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

e_no=(EditText)findViewById(R.id.editText);
e_name=(EditText)findViewById(R.id.editText2);
e_btn=(Button)findViewById(R.id.button);

e_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
no=e_no.getText().toString();
name=e_name.getText().toString();
insert();
}
});
}

private void insert()
{
Thread thread=new Thread(new Runnable() {
@Override
public void run()
{
ArrayList<namevaluepair> nameValuePairs=new ArrayList<namevaluepair>();
nameValuePairs.add(new BasicNameValuePair("no",no));
nameValuePairs.add(new BasicNameValuePair("name",name));
Looper.prepare();
try
{
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://10.0.2.2/insert2.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpClient.execute(httpPost);
HttpEntity entity=response.getEntity();
is=entity.getContent();
Log.d("Pass 1","connection success");
}
catch(Exception e)
{
Toast.makeText(MainActivity.this,"Fail 2 "+e.getMessage().toString(),Toast.LENGTH_LONG).show();
}

try
{
BufferedReader reader=new BufferedReader(new InputStreamReader(is, HTTP.UTF_8),8);
StringBuilder sb=new StringBuilder();
while((line=reader.readLine())!=null)
{
sb.append(line+"\n");
}
is.close();
result=sb.toString();
Toast.makeText(MainActivity.this,"Connection Success",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(MainActivity.this,"Fail 2 "+e.getMessage().toString(),Toast.LENGTH_LONG).show();
}

try
{
JSONObject json_data=new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
Toast.makeText(getBaseContext(), "Inserted Successfully",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Toast.makeText(MainActivity.this,"Fail 3 "+e.getMessage().toString(),Toast.LENGTH_LONG).show();
}

}
});
thread.start();
}

}
Posted
Updated 12-Mar-15 4:33am
v2
Comments
Richard MacCutchan 12-Mar-15 12:45pm    
Are you sure it reaches that point and gets the code value of 1?

1 solution

I suggest to use the AsyncTask to send the data to web service like template code

Java
class Asy extends AsyncTask<string,> {
ProgressDialog pd;

@Override
protected void onPreExecute() {
	// TODO Auto-generated method stub
	super.onPreExecute();
	pd = new ProgressDialog(getActivity());
	pd.setTitle("Order Instructions");
	pd.setMessage("Wait! Getting data...");
	pd.setCancelable(false);
	pd.show();
}

@Override
protected String doInBackground(String... params) {
        // write a method to send http request here
	return sendData();
}

@Override
protected void onPostExecute(String result) {
	// TODO Auto-generated method stub
	super.onPostExecute(result);
        // this return data will be stored in var result
        // parse data here
}
 
Share this answer
 
v3
Comments
jagdish vaghela 13-Mar-15 1:05am    
Thank you. But i don't have a problem in insert data in the last of my code I'm printing a toast message this toast massage not show up when i run the program and data in the database insert successful and it is displayed in mysql database then what the problem into my code everything is going well but toast massage not working
giaosucan 13-Mar-15 4:57am    
Did you debug this line:

JSONObject json_data=new JSONObject(result);
code=(json_data.getInt("code"));
which value display or you got the exception?
Or you may change the "Toast.makeText(getBaseContext()" to "Toast.makeText(MainActivity.this"

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