2013-11-09 148 views
0

我想用android连接MySQL中的数据库,但是当数据在NewProductActivity中输入并点击createproduct按钮时,然后生成错误的可惜已停止。代码如下。在这个数据库路径给出,但我不知道我必须给予哪个路径,因为我用本地主机打开了wamp和数据库。运行这个程序后,点击按钮在数据库指针中插入值进入createProduct类及其方法,但不要进入doInbackground()。用android连接php和mysql数据库

//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` 
+0

PLZ看到这个URL为http://www.androidhive。 info/2012/05/how-to-connect-android-with-php-mysql/ – Avijit

回答

2

显然'localhost'不是您要连接的正确URL。

如果你正在运行的模拟器这个程序,并要连接到Web服务器上运行的PC运行的模拟器,使用10.0.2.2,更多关于这一点 - http://developer.android.com/tools/devices/emulator.html

,如果你不在模拟器上,使用运行Web服务器的机器的实际IP /主机。确保首先可以访问Android设备。

+1

Ben Haim - 谢谢你 – user2893626

0

如果你正在运行的模拟器这个程序我们10.0.2.2,如果你想使用真实的设备,然后使用您的计算机的IP地址192.168.XX