2013-11-09 08:26
浏览 48


I am trying to connect database in MySQL with android,but when data enter in NewProductActivity and click on createproduct button then generate error of Unfortunately has stopped. code given below. In this database path is given but i don't know exactly which path i have to give because i used wamp and database open through localhost. After running this program, click on button to insert value in database pointer go into createProduct class and its method but do not go into doInbackground().

package com.example.projectewithmysql;

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.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";
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        inputName = (EditText) findViewById(;
        inputPrice = (EditText) findViewById(;
        inputDesc = (EditText) findViewById(;

        // Create button
        btnCreateProduct = (Button) findViewById(;
        btnCreateProduct.setOnClickListener(new View.OnClickListener() {

            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;
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "onPreExecute", Toast.LENGTH_LONG).show();
             pDialog = new ProgressDialog(NewProductActivity.this);
                pDialog.setMessage("Creating Product..");
        protected String doInBackground(String... args0) 
            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);

                    // closing this screen
                } else {
                    // failed to create product
            } catch (JSONException e) {
            return null;
        protected void onPostExecute(String result) 
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "onPostExecute", Toast.LENGTH_LONG).show();

 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`
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutu1939
    doutu1939 2013-11-09 08:37

    obviously 'localhost' isn't the correct URL that you want to connect to.

    if you're running this app from an emulator and you want to connect to a web server running on the pc running the emulator, use, more about this -

    if you're not on an emulator, use the actual ip/host of the machine running the web server. make sure its accessible to the Android device first.

    点赞 评论
  • dongraa1986
    dongraa1986 2014-01-03 09:41

    If you're running this app on emulator us and if you want to use on real device then use your computer IP address 192.168.X.X

    点赞 评论