php和mysql数据库与android的连接
我正在尝试将MySQL中的数据库与android连接,但当数据进入NewProductActivity并单击createproduct按钮时,不幸的是,生成错误已停止。代码如下。在这个数据库中,给出了路径,但我不知道应该给出哪条路径,因为我使用了wamp和通过localhost打开的数据库。运行此程序后,单击按钮将数据库指针中的值插入createProduct类及其方法,但不要插入doInbackground()php和mysql数据库与android的连接,php,android,mysql,json,Php,Android,Mysql,Json,我正在尝试将MySQL中的数据库与android连接,但当数据进入NewProductActivity并单击createproduct按钮时,不幸的是,生成错误已停止。代码如下。在这个数据库中,给出了路径,但我不知道应该给出哪条路径,因为我使用了wamp和通过localhost打开的数据库。运行此程序后,单击按钮将数据库指针中的值插入createProduct类及其方法,但不要插入doInbackground() //NewProductActivity 包com.example.project
//NewProductActivity
包com.example.projectewithmysql;
导入java.io.Console;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.AlertDialog.Builder;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.*;
公共类NewProductActivity扩展了活动
{
私有编辑文本输入名;
私人编辑文本输入价格;
私有编辑文本输入描述;
私人按钮产品;
JSONParser JSONParser=新的JSONParser();
私有静态字符串url_create_product=“**localhost/android_coonnect/product.php**”;
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.add_产品);
inputName=(EditText)findViewById(R.id.inputName);
inputPrice=(EditText)findViewById(R.id.inputPrice);
inputDesc=(EditText)findViewById(R.id.inputDesc);
//创建按钮
btnCreateProduct=(按钮)findViewById(R.id.btnCreateProduct);
btnCreateProduct.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
Toast.makeText(getApplicationContext(),“进入按钮”,Toast.LENGTH_LONG.show();
新建CreateNewProduct().execute();
Toast.makeText(getApplicationContext(),“call createNewProduct”,Toast.LENGTH_LONG.show();
}
});
}
类CreateNewProduct扩展了AsyncTask
{
私人对话;
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
Toast.makeText(getApplicationContext(),“onPreExecute”,Toast.LENGTH\u LONG.show();
super.onPreExecute();
pDialog=newprogressdialog(NewProductActivity.this);
pDialog.setMessage(“创建产品…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的字符串背景(字符串…args0)
{
System.out.println(“doInBackground”);
Toast.makeText(getApplicationContext(),“doInBackground”,Toast.LENGTH\u LONG.show();
//TODO自动生成的方法存根
字符串名称=inputName.getText().toString();
字符串价格=inputPrice.getText().toString();
字符串描述=inputDesc.getText().toString();
//建筑参数
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“名称”),名称);
参数添加(新的BasicNameValuePair(“价格”,价格));
参数添加(新的BasicNameValuePair(“说明”,说明));
//获取JSON对象
//请注意,创建产品url接受POST方法
JSONObject json=jsonParser.makeHttpRequest(url\u create\u product,
“POST”,params);
Toast.makeText(getApplicationContext(),“”+url\u create\u product,Toast.LENGTH\u LONG).show();
//检查cat fro响应日志
d(“创建响应”,json.toString());
//检查成功标签
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
//已成功创建产品
Intent i=新Intent(getApplicationContext(),AllProductsActivity.class);
星触觉(i);
//关闭此屏幕
完成();
}否则{
//未能创建产品
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果)
{
//TODO自动生成的方法存根
super.onPostExecute(结果);
Toast.makeText(getApplicationContext(),“onPostExecute”,Toast.LENGTH_LONG.show();
pDialog.disclose();
}
}
}
在这里,我并没有使用web服务器,而是使用json,我给出了有关数据库的信息
我制作了androiddb数据库,表中的产品如下所示
创建表格产品(
pid int(11)主键自动递增,
名称varchar(100)不为空,
价格小数(10,2)不为空,
说明文字,
已在时间戳默认值()处创建_,
在时间戳处更新\u
);
我试图通过android活动输入数据,但生成错误,因此请帮助..`在此处输入代码`
显然,“localhost”不是您要连接到的正确URL
如果您正在从仿真器运行此应用程序,并且希望连接到运行仿真器的pc上运行的web服务器,请使用10.0.2.2,了解更多信息-
如果您不在模拟器上,请使用运行web se的机器的实际ip/主机
//NewProductActivity
package com.example.projectewithmysql;
import java.io.Console;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
public class NewProductActivity extends Activity
{
private EditText inputName;
private EditText inputPrice;
private EditText inputDesc;
private Button btnCreateProduct;
JSONParser jsonParser = new JSONParser();
private static String url_create_product = "**localhost/android_coonect/product.php**";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_product);
inputName = (EditText) findViewById(R.id.inputName);
inputPrice = (EditText) findViewById(R.id.inputPrice);
inputDesc = (EditText) findViewById(R.id.inputDesc);
// Create button
btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Enter into button",Toast.LENGTH_LONG).show();
new CreateNewProduct().execute();
Toast.makeText(getApplicationContext(), "call createNewProduct",Toast.LENGTH_LONG).show();
}
});
}
class CreateNewProduct extends AsyncTask<String, String, String>
{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onPreExecute", Toast.LENGTH_LONG).show();
super.onPreExecute();
pDialog = new ProgressDialog(NewProductActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args0)
{
System.out.println("doInBackground");
Toast.makeText(getApplicationContext(), "doInBackground", Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
String name = inputName.getText().toString();
String price = inputPrice.getText().toString();
String description = inputDesc.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", description));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
Toast.makeText(getApplicationContext(), ""+url_create_product, Toast.LENGTH_LONG).show();
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), "onPostExecute", Toast.LENGTH_LONG).show();
pDialog.dismiss();
}
}
}
here i am not used web-server but using json and I give information about database
I have made androiddb database and table is products that is given below
CREATE TABLE products(
pid int(11) primary key auto_increment,
name varchar(100) not null,
price decimal(10,2) not null,
description text,
created_at timestamp default now(),
updated_at timestamp
);
I try to enter data through android activity but error generate so please help..`enter code here`