Java Android Php MySQL连接成功,但未插入数据
下面是我的代码。连接成功,但未在表中插入值。Java Android Php MySQL连接成功,但未插入数据,java,android,mysql,apache,Java,Android,Mysql,Apache,下面是我的代码。连接成功,但未在表中插入值。 我已经检查了结果字符串-它正在打印{“code”:0} 请帮我修一下 @TargetApi(Build.VERSION\u code.GINGERBREAD)@SuppressLint(“NewApi”)公共类MainActivity扩展活动{ 字符串名; 字符串id; InputStream=null; 字符串结果=null; 字符串行=null; int代码; @SuppressLint(“NewApi”)@覆盖 创建时的公共void(Bundl
我已经检查了结果
字符串
-它正在打印{“code”:0}
请帮我修一下
@TargetApi(Build.VERSION\u code.GINGERBREAD)@SuppressLint(“NewApi”)公共类MainActivity扩展活动{
字符串名;
字符串id;
InputStream=null;
字符串结果=null;
字符串行=null;
int代码;
@SuppressLint(“NewApi”)@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
最终EditText e_id=(EditText)findViewById(R.id.editText1);
最终EditText e_name=(EditText)findViewById(R.id.editText2);
按钮插入=(按钮)findViewById(R.id.button1);
insert.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
id=e_id.getText().toString();
name=e_name.getText().toString();
插入();
}
});
}
公开作废插入()
{
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“id”,id));
添加(新的BasicNameValuePair(“name”,name));
尝试
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2/insert.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
Log.e(“通过1”,“连接成功”);
}
捕获(例外e)
{
Log.e(“Fail 1”,e.toString());
Toast.makeText(getApplicationContext(),“无效IP地址”+e,
Toast.LENGTH_LONG).show();
}
尝试
{
BufferedReader reader=新的BufferedReader
(新的InputStreamReader(is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
Toast.makeText(getApplicationContext(),“连接成功”+结果,70).show();
Log.e(“通过2”,“连接成功”);
}
捕获(例外e)
{
Toast.makeText(getApplicationContext(),“连接未成功”,70).show();
Log.e(“Fail 2”,e.toString());
}
尝试
{
JSONObject json_data=新的JSONObject(结果);
code=(json_data.getInt(“code”);
如果(代码==1)
{
Toast.makeText(getBaseContext(),“已成功插入”,
吐司。长度(短)。show();
}
其他的
{
Toast.makeText(getBaseContext(),“抱歉,再试一次”+代码,
Toast.LENGTH_LONG).show();
}
}
捕获(例外e)
{
Log.e(“Fail 3”,e.toString());
}
}
}
也许php页面就是问题所在发布您的php代码以及您在数据库中使用的内容。SQLite还是MySQL??
String name;
String id;
InputStream is=null;
String result=null;
String line=null;
int code;
@SuppressLint("NewApi") @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
final EditText e_id=(EditText) findViewById(R.id.editText1);
final EditText e_name=(EditText) findViewById(R.id.editText2);
Button insert=(Button) findViewById(R.id.button1);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
id = e_id.getText().toString();
name = e_name.getText().toString();
insert();
}
});
}
public void insert()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",id));
nameValuePairs.add(new BasicNameValuePair("name",name));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address"+e,
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Toast.makeText(getApplicationContext(), "Connection Success "+result, 70).show();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Connection Not Successfull", 70).show();
Log.e("Fail 2", e.toString());
}
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"+code,
Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
}