数据成功插入到MySql数据库但无法显示Toast按摩如何解决这个问题? [英] Data successfully inserted into MySql database but can't display Toast massage how to solve this problem?
问题描述
这是我用PHP和android将数据插入Mysql数据库的代码我成功完全将数据插入数据库但是它无法显示Toast massage ofInserted Successfully是什么我在编码中遇到的问题请任何人帮助我
这里是我的PHP文件,用于插入数据..
insert.php
.. ..........
header('Content-type = application / json; charset = utf-8');
$ con = mysql_connect( '127.0.0.1','root','')或死(连接失败);
$ db = mysql_select_db(Android_Database,$ con)或死掉(找不到数据库! );
$ no = $ _ REQUEST ['no'];
$ name = $ _ REQUEST ['name']; < br $>
$ flag ['code'] = 0;
if($ query = mysql_query(insert进入信息值('$ no','$ name'),$ con))
{
$ flag ['code'] = 1;
}
print(json_encode($ flag));
mysql_close($ con);
? >
............
这是我的Android活动文件
MainActivity.java
................................
公共类MainActivity扩展ActionBarActivity
{
按钮e_btn;
EditText e_no,e_name;
字符串号,名称;
InputStream是;
字符串结果= null;
字符串行= 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(查看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 ,名称));
Looper.prepare();
尝试
{
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(通过1,连接成功);
}
catch(例外e)
{
Toast.makeText(MainActivity.this,Fail 2+ e.getMessage()。toString(),Toast.LENGTH_LONG)。show();
}
尝试
{
BufferedReader reader = new BufferedReade r(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(例外e)
{
Toast.makeText(MainActivity.this,失败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.m akeText(getBaseContext(),抱歉,再试一次,
Toast.LENGTH_LONG)。show();
}
}
catch(例外e)
{
Toast.makeText(MainActivity.this,Fail 3+ e.getMessage()。toString() ,Toast.LENGTH_LONG)。show();
}
}
});
thread.start();
}
}
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();
}
}
推荐答案
con = mysql_connect('127.0.0.1','root','')或die(Connection Failed);
con=mysql_connect('127.0.0.1','root','') or die("Connection Failed");
db = mysql_select_db( Android_Database,
db=mysql_select_db("Android_Database",
con)或死掉(找不到数据库!);
con) or die("Database not found!");
这篇关于数据成功插入到MySql数据库但无法显示Toast按摩如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!