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();
}
}