问:
how to build wget progress bar in Node JS for downloading files?
I am writing a command line tool, which downloads a software from the net. request and request-progress modules to download. The code is below(same as official documented one).
Now I want to show the progress of download something similar to wget download progress bar?

```<code>var fs = require('fs');
var request = require('request');
var progress = require('request-progress');

// The options argument is optional so you can omit it
progress(request('https://az412801.vo.msecnd.net/vhd/VMBuild_20141027/VirtualBox/IE11/Windows/IE11.Win8.1.For.Windows.VirtualBox.zip'), {
    // throttle: 2000,                    // Throttle the progress event to 2000ms, defaults to 1000ms
    // delay: 1000,                       // Only start to emit after 1000ms delay, defaults to 0ms
    // lengthHeader: 'x-transfer-length'  // Length header to use, defaults to content-length
})
.on('progress', function (state) {
    // The state is an object that looks like this:
    // {
    //     percent: 0.5,               // Overall percent (between 0 to 1)
    //     speed: 554732,              // The download speed in bytes/sec
    //     size: {
    //         total: 90044871,        // The total payload size in bytes
    //         transferred: 27610959   // The transferred payload size in bytes
    //     },
    //     time: {
    //         elapsed: 36.235,        // The total elapsed seconds since the start (3 decimals)
    //         remaining: 81.403       // The remaining seconds to finish (3 decimals)
    //     }
    // }
    console.log('progress', state);
})
.on('error', function (err) {
    // Do something with err
})
.on('end', function () {
    // Do something after request finishes
})
.pipe(fs.createWriteStream('IE11.Win8.1.For.Windows.VirtualBox.zip'));
</code>```

Progress bar for above program

How to get the progress bar some this similar to wget in nodejs? Simply to understand I added wget screenshots for same file download. It's simple and efficient.

答:
Here's a working example but not polished, you might need to tweak some stuff:

```<code>const fs = require('fs');
const request = require('request');
const progress = require('request-progress');
const readline = require('readline')

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const filename = 'IE11.Win8.1.For.Windows.VirtualBox.zip';

progress(request('https://az412801.vo.msecnd.net/vhd/VMBuild_20141027/VirtualBox/IE11/Windows/IE11.Win8.1.For.Windows.VirtualBox.zip'), {
})
.on('progress', function (state) {
  readline.clearLine(rl, 0);
  readline.cursorTo(rl, 0);
  rl.write(statusBar(filename, state));
})
.on('error', function (err) {
    // Do something with err
})
.on('end', function () {
    // Do something after request finishes
})
.pipe(fs.createWriteStream(filename));

function statusBar(filename, progress) {
  let str = `${filename}    `;
  str += progressBar(progress.percent);
  str += stats(progress);
  return str;
}

function progressBar(percent) {
  percent = Math.floor(percent * 100);
  let str = `%${percent}[`;
  str += '='.repeat(percent);
  str += '>'
  str += ' '.repeat(99 - percent);
  str += '] ';
  return str
}

function stats({speed, size, time}) {
  const sizeM = Math.round(size.transferred / 1024)
  const speedMb = Math.round(speed * 0.000008)
  const eta = new Date(time.remaining * 1000).toISOString().substr(11, 8);
  return `${sizeM}M ${speedMb}MB/s eta ${eta}`;
}
</code>```

NOTE:

I'm using some ES6 features, like:

String.prototype.repeat

Template Literals

Destructuring assignment (on the function stats)

I think the code is self explanatory, make sure you give a look at the readline docs

Screenshot:

Body:
I am writing a command line tool, which downloads a software from the net. request and request-progress modules to download. The code is below(same as official documented one).
Now I want to show the progress of download something similar to wget download progress bar?

var fs = require('fs');
var request = require('request');
var progress = require('request-progress');

// The options argument is optional so you can omit it
progress(request('https://az412801.vo.msecnd.net/vhd/VMBuild_20141027/VirtualBox/IE11/Windows/IE11.Win8.1.For.Windows.VirtualBox.zip'), {
    // throttle: 2000,                    // Throttle the progress event to 2000ms, defaults to 1000ms
    // delay: 1000,                       // Only start to emit after 1000ms delay, defaults to 0ms
    // lengthHeader: 'x-transfer-length'  // Length header to use, defaults to content-length
})
.on('progress', function (state) {
    // The state is an object that looks like this:
    // {
    //     percent: 0.5,               // Overall percent (between 0 to 1)
    //     speed: 554732,              // The download speed in bytes/sec
    //     size: {
    //         total: 90044871,        // The total payload size in bytes
    //         transferred: 27610959   // The transferred payload size in bytes
    //     },
    //     time: {
    //         elapsed: 36.235,        // The total elapsed seconds since the start (3 decimals)
    //         remaining: 81.403       // The remaining seconds to finish (3 decimals)
    //     }
    // }
    console.log('progress', state);
})
.on('error', function (err) {
    // Do something with err
})
.on('end', function () {
    // Do something after request finishes
})
.pipe(fs.createWriteStream('IE11.Win8.1.For.Windows.VirtualBox.zip'));

Progress bar for above program

How to get the progress bar some this similar to wget in nodejs? Simply to understand I added wget screenshots for same file download. It's simple and efficient.

Body:
Here's a working example but not polished, you might need to tweak some stuff:

const fs = require('fs');
const request = require('request');
const progress = require('request-progress');
const readline = require('readline')

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const filename = 'IE11.Win8.1.For.Windows.VirtualBox.zip';

progress(request('https://az412801.vo.msecnd.net/vhd/VMBuild_20141027/VirtualBox/IE11/Windows/IE11.Win8.1.For.Windows.VirtualBox.zip'), {
})
.on('progress', function (state) {
  readline.clearLine(rl, 0);
  readline.cursorTo(rl, 0);
  rl.write(statusBar(filename, state));
})
.on('error', function (err) {
    // Do something with err
})
.on('end', function () {
    // Do something after request finishes
})
.pipe(fs.createWriteStream(filename));

function statusBar(filename, progress) {
  let str = `${filename}    `;
  str += progressBar(progress.percent);
  str += stats(progress);
  return str;
}

function progressBar(percent) {
  percent = Math.floor(percent * 100);
  let str = `%${percent}[`;
  str += '='.repeat(percent);
  str += '>'
  str += ' '.repeat(99 - percent);
  str += '] ';
  return str
}

function stats({speed, size, time}) {
  const sizeM = Math.round(size.transferred / 1024)
  const speedMb = Math.round(speed * 0.000008)
  const eta = new Date(time.remaining * 1000).toISOString().substr(11, 8);
  return `${sizeM}M ${speedMb}MB/s eta ${eta}`;
}

NOTE:

I'm using some ES6 features, like:

String.prototype.repeat

Template Literals

Destructuring assignment (on the function stats)

I think the code is self explanatory, make sure you give a look at the readline docs

Screenshot:


来源: stackexchange

问:
Data Get Repeated in Android ListView
I've just started learning android programming specifically for a project and stuck into a very basic thing though this shouldn't be. The issue is I am using Sql database to show product details in Android ListView. This is working fine. Again I am adding product data using button click. When I add product, the ListView gets repeated. Like along with newly added product, the previous product details get repeated twice in the ListView as the following image shows:

Note: In the image, Nokia (Nokia Lumia) is the newly entered product.

In the Add button, I've just called the ShowProducts method to refresh the ListView as follows:

```<code>if(isSuccess == true) {
     ShowProducts showProducts = new ShowProducts();
     showProducts.execute();
}
</code>```

I am not sure why it's happening. Debugged but unable to figure it out. Even used SimpleAdapter's notifyDataSetChanged method for refreshing but didn't work. Here is the table structure:

```<code>CREATE TABLE [dbo].[Products](
 [ProductId] [int] IDENTITY(1,1) PRIMARY KEY,
 [ProductName] [nvarchar](60) NOT NULL,
 [Code] [nvarchar](60) NOT NULL,
 [Code] [nvarchar](60) NOT NULL,
 [Date] [datetime] NOT NULL
)
</code>```

In the project folder, app\src\main\java\com.example.at.projectname folder, created several classes:

Products.java:

```<code>public class Products {
    private String code;
    private String name;
    private String price;

    public String getCode(String productCode) {
        code = productCode;
        return code;
    }

    public String getName(String productName) {
        name = productName;
        return name;
    }

    public String getPrice(String productPrice) {
        price = productPrice;
        return price;
    }
}
</code>```

ConnectionClass.java:

```<code>public class ConnectionClass {
    String ip = "";
    String classs = "net.sourceforge.jtds.jdbc.Driver";
    String db = "sampleDB";

    @SuppressLint("NewApi")
    public Connection CONN() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();

        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        String ConnURL = null;

        try {
            Class.forName(classs);
            ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                    + "databaseName=" + db + ";user=" + ";password="
                    + ";";
            conn = DriverManager.getConnection(ConnURL);
        } catch (SQLException se) {
            Log.e("Error: ", se.getMessage());
        } catch (ClassNotFoundException e) {
            Log.e("Error: ", e.getMessage());
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return conn;
    }
}
</code>```

ProductActivity.java:

```<code>public class ProductActivity extends Activity {

    ConnectionClass connectionClass;
    EditText edtpProductName, edtProductCode, edtProductPrice;
    Button btnAdd, btnUpdate, btnDelete;
    ProgressBar pbbar;
    ListView lstProduct;
    String productCode;

    List<Map<String, String>> productList = new ArrayList<Map<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.products);

        connectionClass = new ConnectionClass();

        edtpProductName = (EditText) findViewById(R.id.edtProductName);
        edtProductCode = (EditText) findViewById(R.id.edtProductCode);
        edtProductPrice = (EditText) findViewById(R.id.edtProductPrice);

        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnUpdate = (Button) findViewById(R.id.btnUpdate);
        btnDelete = (Button) findViewById(R.id.btnDelete);

        pbbar = (ProgressBar) findViewById(R.id.pbbar);
        pbbar.setVisibility(View.GONE);

        lstProduct = (ListView) findViewById(R.id.lstProducts);

        productCode = "";

        lstProduct.addHeaderView(getLayoutInflater().inflate(R.layout.header, null, false));

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddProducts addProducts = new AddProducts();
                addProducts.execute();
            }
        });

        ShowProducts showProducts = new ShowProducts();
        showProducts.execute();
    }

    public class ShowProducts extends AsyncTask<String, String, String> {
        String msg = "";

        @Override
        protected void onPreExecute() {
            pbbar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String r) {
            pbbar.setVisibility(View.GONE);
            Toast.makeText(ProductActivity.this, r, Toast.LENGTH_SHORT).show();

            final Products products = new Products();

            String[] from = { products.getCode("Code"), products.getName("ProductName"), products.getPrice("Price") };
            int[] views = {R.id.lblProductCode, R.id.lblProductName, R.id.lblProductPrice};
            final SimpleAdapter ADA = new SimpleAdapter(ProductActivity.this, productList, R.layout.lsttemplate, from, views);
            lstProduct.setAdapter(ADA);

            ADA.notifyDataSetChanged();

            lstProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                    HashMap<String, Object> obj = (HashMap<String, Object>) ADA
                            .getItem(arg2);

                    productCode = (String) obj.get(products.getCode("Code"));
                    String productName = (String) obj.get(products.getName("ProductName"));
                    String productPrice = (String) obj.get(products.getPrice("Price"));

                    edtProductCode.setText(productCode);
                    edtpProductName.setText(productName);
                }
            });
        }

        @Override
        protected String doInBackground(String... params) {

            try {
                Connection con = connectionClass.CONN();
                if (con == null) {
                    msg = "Error in connection with SQL server";
                } else {
                    String query = "SELECT ProductName, Code, Price FROM Products";
                    PreparedStatement ps = con.prepareStatement(query);
                    ResultSet rs = ps.executeQuery();

                    while (rs.next()) {
                        Products products = new Products();
                        Map<String, String> productList = new HashMap<String, String>();

                        String code = products.getCode(rs.getString("Code"));
                        String name = products.getName(rs.getString("ProductName"));
                        String price = products.getName(rs.getString("Price"));

                        productList.put("Code", code);
                        productList.put("ProductName", name);
                        productList.put("Price", price);

                        ProductActivity.this.productList.add(productList);
                    }
                    msg = "Success";
                }
            } catch (Exception ex) {
                msg = "Error retrieving data from table";
            }
            return msg;
        }
    }

    public class AddProducts extends AsyncTask<String, String, String> {
        String msg = "";
        Boolean isSuccess = false;

        String proname = edtpProductName.getText().toString();
        String procode = edtProductCode.getText().toString();
        String proprice = edtProductPrice.getText().toString();

        @Override
        protected void onPreExecute() {
            pbbar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String r) {
            pbbar.setVisibility(View.GONE);
            Toast.makeText(ProductActivity.this, r, Toast.LENGTH_SHORT).show();

            if(isSuccess == true) {
                ShowProducts showProducts = new ShowProducts();
                showProducts.execute();
            }
        }

        @Override
        protected String doInBackground(String... params) {
            if (proname.trim().equals("")) {
                msg = "Please enter product name!";
            } else if (procode.trim().equals("")) {
                msg = "Please enter product code!";
            } else if (proprice.trim().equals("")) {
                msg = "Please enter product price!";
            } else {
                try {
                    Connection con = connectionClass.CONN();
                    if (con == null) {
                        msg = "Error in connection with SQL server";
                    } else {

                        String dates = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).format(Calendar.getInstance().getTime());
                        String query = "INSERT INTO Products (ProductName, Code, Price, Date) VALUES ('" + proname + "', '" + procode + "', '" + proprice + "', '" + dates + "')";

                        PreparedStatement ps = con.prepareStatement(query);
                        ps.executeUpdate();

                        msg = "Added Successfully";
                        isSuccess = true;
                    }
                } catch (Exception ex) {
                    isSuccess = false;
                    msg = "Exceptions";
                }
            }
            return msg;
        }
    }
}
</code>```

Finally comes the layouts. There are two layouts. i) products.xml - The main layout ii) lsttemplate.xml - For the modification of the ListView:

products.xml

```<code><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#282828"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:padding="2dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name_addproducts"
            android:layout_marginTop="7dp"
            android:typeface="sans"
            android:textSize="35sp"
            android:textColor="#ffffff"
            android:gravity="center" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:textStyle="bold"
            android:background="#5d5d5d"
            android:padding="10dp"
            android:hint="@string/app_name_addproductName"
            android:textSize="20sp"
            android:id="@+id/edtProductName" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="@string/app_name_addproductCode"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:textStyle="bold"
            android:background="#5d5d5d"
            android:padding="10dp"
            android:inputType="textMultiLine"
            android:maxLines="3"
            android:minLines="2"
            android:layout_gravity="top|left"
            android:layout_marginTop="5dp"
            android:id="@+id/edtProductCode" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="@string/app_name_addproductPrice"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:textStyle="bold"
            android:background="#5d5d5d"
            android:padding="10dp"
            android:inputType="textMultiLine"
            android:maxLines="3"
            android:minLines="2"
            android:layout_gravity="top|left"
            android:layout_marginTop="5dp"
            android:id="@+id/edtProductPrice" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorDarkForest"
                android:layout_weight="1"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:layout_margin="2dp"
                android:padding="7dp"
                android:layout_marginTop="10dp"
                android:id="@+id/btnAdd"
                android:text="@string/app_name_addproduct" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorDarkForest"
                android:layout_weight="1"
                android:layout_margin="2dp"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:padding="7dp"
                android:layout_marginTop="10dp"
                android:id="@+id/btnUpdate"
                android:text="@string/app_name_updateproduct" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorDarkForest"
                android:layout_weight="1"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:layout_margin="2dp"
                android:padding="7dp"
                android:layout_marginTop="10dp"
                android:id="@+id/btnDelete"
                android:text="@string/app_name_deleteproduct" />
        </LinearLayout>

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/pbbar" />

        <ListView
            android:id="@+id/lstProducts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="#808080"
            android:layout_marginTop="5dp"
            android:dividerHeight="1dp"
            android:padding="5dp">
        </ListView>
    </LinearLayout>
</RelativeLayout>
</code>```

lsttemplate.xml:

```<code><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="5"
    android:padding="5dp"
    android:layout_marginTop="2dp"
    >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:layout_weight="1"
        android:textSize="15sp"
        android:id="@+id/lblProductCode"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:width="40dip"
        android:id="@+id/lblProductName"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:paddingLeft="25dp"
        android:id="@+id/lblProductPrice"/>
</LinearLayout>
</code>```

答:
Just add the bellow code in onPreExecute

```<code>try{
    productList.clear();
    lstProduct.removeAllViews();
}catch (Exception e){

}
</code>```

You can also use custom adapter with ArrayAdapter

ContentLicense: CC BY-SA 3.0

Title: Data Get Repeated in Android ListView

Body:
I've just started learning android programming specifically for a project and stuck into a very basic thing though this shouldn't be. The issue is I am using Sql database to show product details in Android ListView. This is working fine. Again I am adding product data using button click. When I add product, the ListView gets repeated. Like along with newly added product, the previous product details get repeated twice in the ListView as the following image shows:

Note: In the image, Nokia (Nokia Lumia) is the newly entered product.

In the Add button, I've just called the ShowProducts method to refresh the ListView as follows:

if(isSuccess == true) {
     ShowProducts showProducts = new ShowProducts();
     showProducts.execute();
}

I am not sure why it's happening. Debugged but unable to figure it out. Even used SimpleAdapter's notifyDataSetChanged method for refreshing but didn't work. Here is the table structure:

CREATE TABLE [dbo].[Products](
 [ProductId] [int] IDENTITY(1,1) PRIMARY KEY,
 [ProductName] [nvarchar](60) NOT NULL,
 [Code] [nvarchar](60) NOT NULL,
 [Code] [nvarchar](60) NOT NULL,
 [Date] [datetime] NOT NULL
)

In the project folder, app\src\main\java\com.example.at.projectname folder, created several classes:

Products.java:

public class Products {
    private String code;
    private String name;
    private String price;

    public String getCode(String productCode) {
        code = productCode;
        return code;
    }

    public String getName(String productName) {
        name = productName;
        return name;
    }

    public String getPrice(String productPrice) {
        price = productPrice;
        return price;
    }
}

ConnectionClass.java:

public class ConnectionClass {
    String ip = "";
    String classs = "net.sourceforge.jtds.jdbc.Driver";
    String db = "sampleDB";

    @SuppressLint("NewApi")
    public Connection CONN() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();

        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        String ConnURL = null;

        try {
            Class.forName(classs);
            ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                    + "databaseName=" + db + ";user=" + ";password="
                    + ";";
            conn = DriverManager.getConnection(ConnURL);
        } catch (SQLException se) {
            Log.e("Error: ", se.getMessage());
        } catch (ClassNotFoundException e) {
            Log.e("Error: ", e.getMessage());
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return conn;
    }
}

ProductActivity.java:

public class ProductActivity extends Activity {

    ConnectionClass connectionClass;
    EditText edtpProductName, edtProductCode, edtProductPrice;
    Button btnAdd, btnUpdate, btnDelete;
    ProgressBar pbbar;
    ListView lstProduct;
    String productCode;

    List<Map<String, String>> productList = new ArrayList<Map<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.products);

        connectionClass = new ConnectionClass();

        edtpProductName = (EditText) findViewById(R.id.edtProductName);
        edtProductCode = (EditText) findViewById(R.id.edtProductCode);
        edtProductPrice = (EditText) findViewById(R.id.edtProductPrice);

        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnUpdate = (Button) findViewById(R.id.btnUpdate);
        btnDelete = (Button) findViewById(R.id.btnDelete);

        pbbar = (ProgressBar) findViewById(R.id.pbbar);
        pbbar.setVisibility(View.GONE);

        lstProduct = (ListView) findViewById(R.id.lstProducts);

        productCode = "";

        lstProduct.addHeaderView(getLayoutInflater().inflate(R.layout.header, null, false));

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddProducts addProducts = new AddProducts();
                addProducts.execute();
            }
        });

        ShowProducts showProducts = new ShowProducts();
        showProducts.execute();
    }

    public class ShowProducts extends AsyncTask<String, String, String> {
        String msg = "";

        @Override
        protected void onPreExecute() {
            pbbar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String r) {
            pbbar.setVisibility(View.GONE);
            Toast.makeText(ProductActivity.this, r, Toast.LENGTH_SHORT).show();

            final Products products = new Products();

            String[] from = { products.getCode("Code"), products.getName("ProductName"), products.getPrice("Price") };
            int[] views = {R.id.lblProductCode, R.id.lblProductName, R.id.lblProductPrice};
            final SimpleAdapter ADA = new SimpleAdapter(ProductActivity.this, productList, R.layout.lsttemplate, from, views);
            lstProduct.setAdapter(ADA);

            ADA.notifyDataSetChanged();

            lstProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                    HashMap<String, Object> obj = (HashMap<String, Object>) ADA
                            .getItem(arg2);

                    productCode = (String) obj.get(products.getCode("Code"));
                    String productName = (String) obj.get(products.getName("ProductName"));
                    String productPrice = (String) obj.get(products.getPrice("Price"));

                    edtProductCode.setText(productCode);
                    edtpProductName.setText(productName);
                }
            });
        }

        @Override
        protected String doInBackground(String... params) {

            try {
                Connection con = connectionClass.CONN();
                if (con == null) {
                    msg = "Error in connection with SQL server";
                } else {
                    String query = "SELECT ProductName, Code, Price FROM Products";
                    PreparedStatement ps = con.prepareStatement(query);
                    ResultSet rs = ps.executeQuery();

                    while (rs.next()) {
                        Products products = new Products();
                        Map<String, String> productList = new HashMap<String, String>();

                        String code = products.getCode(rs.getString("Code"));
                        String name = products.getName(rs.getString("ProductName"));
                        String price = products.getName(rs.getString("Price"));

                        productList.put("Code", code);
                        productList.put("ProductName", name);
                        productList.put("Price", price);

                        ProductActivity.this.productList.add(productList);
                    }
                    msg = "Success";
                }
            } catch (Exception ex) {
                msg = "Error retrieving data from table";
            }
            return msg;
        }
    }

    public class AddProducts extends AsyncTask<String, String, String> {
        String msg = "";
        Boolean isSuccess = false;

        String proname = edtpProductName.getText().toString();
        String procode = edtProductCode.getText().toString();
        String proprice = edtProductPrice.getText().toString();

        @Override
        protected void onPreExecute() {
            pbbar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String r) {
            pbbar.setVisibility(View.GONE);
            Toast.makeText(ProductActivity.this, r, Toast.LENGTH_SHORT).show();

            if(isSuccess == true) {
                ShowProducts showProducts = new ShowProducts();
                showProducts.execute();
            }
        }

        @Override
        protected String doInBackground(String... params) {
            if (proname.trim().equals("")) {
                msg = "Please enter product name!";
            } else if (procode.trim().equals("")) {
                msg = "Please enter product code!";
            } else if (proprice.trim().equals("")) {
                msg = "Please enter product price!";
            } else {
                try {
                    Connection con = connectionClass.CONN();
                    if (con == null) {
                        msg = "Error in connection with SQL server";
                    } else {

                        String dates = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).format(Calendar.getInstance().getTime());
                        String query = "INSERT INTO Products (ProductName, Code, Price, Date) VALUES ('" + proname + "', '" + procode + "', '" + proprice + "', '" + dates + "')";

                        PreparedStatement ps = con.prepareStatement(query);
                        ps.executeUpdate();

                        msg = "Added Successfully";
                        isSuccess = true;
                    }
                } catch (Exception ex) {
                    isSuccess = false;
                    msg = "Exceptions";
                }
            }
            return msg;
        }
    }
}

Finally comes the layouts. There are two layouts. i) products.xml - The main layout ii) lsttemplate.xml - For the modification of the ListView:

products.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#282828"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:padding="2dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name_addproducts"
            android:layout_marginTop="7dp"
            android:typeface="sans"
            android:textSize="35sp"
            android:textColor="#ffffff"
            android:gravity="center" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:textStyle="bold"
            android:background="#5d5d5d"
            android:padding="10dp"
            android:hint="@string/app_name_addproductName"
            android:textSize="20sp"
            android:id="@+id/edtProductName" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="@string/app_name_addproductCode"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:textStyle="bold"
            android:background="#5d5d5d"
            android:padding="10dp"
            android:inputType="textMultiLine"
            android:maxLines="3"
            android:minLines="2"
            android:layout_gravity="top|left"
            android:layout_marginTop="5dp"
            android:id="@+id/edtProductCode" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="@string/app_name_addproductPrice"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:textStyle="bold"
            android:background="#5d5d5d"
            android:padding="10dp"
            android:inputType="textMultiLine"
            android:maxLines="3"
            android:minLines="2"
            android:layout_gravity="top|left"
            android:layout_marginTop="5dp"
            android:id="@+id/edtProductPrice" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorDarkForest"
                android:layout_weight="1"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:layout_margin="2dp"
                android:padding="7dp"
                android:layout_marginTop="10dp"
                android:id="@+id/btnAdd"
                android:text="@string/app_name_addproduct" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorDarkForest"
                android:layout_weight="1"
                android:layout_margin="2dp"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:padding="7dp"
                android:layout_marginTop="10dp"
                android:id="@+id/btnUpdate"
                android:text="@string/app_name_updateproduct" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorDarkForest"
                android:layout_weight="1"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:layout_margin="2dp"
                android:padding="7dp"
                android:layout_marginTop="10dp"
                android:id="@+id/btnDelete"
                android:text="@string/app_name_deleteproduct" />
        </LinearLayout>

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/pbbar" />

        <ListView
            android:id="@+id/lstProducts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="#808080"
            android:layout_marginTop="5dp"
            android:dividerHeight="1dp"
            android:padding="5dp">
        </ListView>
    </LinearLayout>
</RelativeLayout>

lsttemplate.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="5"
    android:padding="5dp"
    android:layout_marginTop="2dp"
    >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:layout_weight="1"
        android:textSize="15sp"
        android:id="@+id/lblProductCode"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:width="40dip"
        android:id="@+id/lblProductName"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:paddingLeft="25dp"
        android:id="@+id/lblProductPrice"/>
</LinearLayout>

Body:
Just add the bellow code in onPreExecute

try{
    productList.clear();
    lstProduct.removeAllViews();
}catch (Exception e){

}

You can also use custom adapter with ArrayAdapter


问:
Spring Integration HTTP Outbound Gateway Post Request with Java DSL
I am trying to consume a rest service and receive a json back and convert it to a list of objects. but I am receiving the below erorr. I am new to EIP and there aren't many tutorials for doing this in java dsl. I have configured 2 channels, one for sending a request and one for receiving the payload back.

```<code>Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'httpPostAtms' is expected to be of type 'org.springframework.messaging.MessageChannel' but was actually of type 'org.springframework.integration.dsl.StandardIntegrationFlow'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:378)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:89)
at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:46)
at org.springframework.integration.gateway.MessagingGatewaySupport.getRequestChannel(MessagingGatewaySupport.java:344)
at org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:433)
at org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceive(MessagingGatewaySupport.java:422)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:474)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:429)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:420)
at org.springframework.integration.gateway.GatewayCompletableFutureProxyFactoryBean.invoke(GatewayCompletableFutureProxyFactoryBean.java:65)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy70.getAllAtms(Unknown Source)
at com.backbase.atm.IngAtmApplication.main(IngAtmApplication.java:25)
</code>```

I am using SI with Spring Boot

```<code>@IntegrationComponentScan
@Configuration
@EnableIntegration
@ComponentScan
public class InfrastructorConfig {

    @Bean
    public PollableChannel requestChannel() {
        return new PriorityChannel() ;
    }

    @Bean
    public MessageChannel replyChannel() {
        return new DirectChannel() ;
    }

    @Bean(name = PollerMetadata.DEFAULT_POLLER)
    public PollerMetadata poller() {
        return Pollers.fixedRate(500).get();
    }

    @Bean
    public IntegrationFlow httpPostAtms() {

        return IntegrationFlows.from("requestChannel")
                .handle(Http.outboundGateway("https://www.ing.nl/api/locator/atms/")
                        .httpMethod(HttpMethod.POST)
                        .extractPayload(true))
                .<String, String>transform(p -> p.substring(5))
                .transform(Transformers.fromJson(Atm[].class))
                .channel("responseChannel")
                .get();

      }

    }
</code>```

The Gateway

```<code>package com.backbase.atm.service;

import java.util.List;

import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.messaging.handler.annotation.Payload;

import com.backbase.atm.model.Atm;

@MessagingGateway
public interface IntegrationService {

    @Gateway(requestChannel = "httpPostAtms")
    @Payload("new java.util.Date()")
    List<Atm> getAllAtms();
}
</code>```

Application Start

```<code>package com.backbase.atm;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import com.backbase.atm.service.IntegrationService;

@SpringBootApplication
public class IngAtmApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(IngAtmApplication.class, args);
        ctx.getBean(IntegrationService.class).getAllAtms();
        ctx.close();
    }
</code>```

答:
You have to use requestChannel bean name in the gateway definition. Right now you have there an IntegrationFlow bean name, but that is wrong.

Always remember that everything in Spring Integration are connected via channels.

Body:
I am trying to consume a rest service and receive a json back and convert it to a list of objects. but I am receiving the below erorr. I am new to EIP and there aren't many tutorials for doing this in java dsl. I have configured 2 channels, one for sending a request and one for receiving the payload back.

Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'httpPostAtms' is expected to be of type 'org.springframework.messaging.MessageChannel' but was actually of type 'org.springframework.integration.dsl.StandardIntegrationFlow'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:378)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:89)
at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:46)
at org.springframework.integration.gateway.MessagingGatewaySupport.getRequestChannel(MessagingGatewaySupport.java:344)
at org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:433)
at org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceive(MessagingGatewaySupport.java:422)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:474)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:429)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:420)
at org.springframework.integration.gateway.GatewayCompletableFutureProxyFactoryBean.invoke(GatewayCompletableFutureProxyFactoryBean.java:65)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy70.getAllAtms(Unknown Source)
at com.backbase.atm.IngAtmApplication.main(IngAtmApplication.java:25)

I am using SI with Spring Boot

@IntegrationComponentScan
@Configuration
@EnableIntegration
@ComponentScan
public class InfrastructorConfig {

    @Bean
    public PollableChannel requestChannel() {
        return new PriorityChannel() ;
    }

    @Bean
    public MessageChannel replyChannel() {
        return new DirectChannel() ;
    }

    @Bean(name = PollerMetadata.DEFAULT_POLLER)
    public PollerMetadata poller() {
        return Pollers.fixedRate(500).get();
    }

    @Bean
    public IntegrationFlow httpPostAtms() {

        return IntegrationFlows.from("requestChannel")
                .handle(Http.outboundGateway("https://www.ing.nl/api/locator/atms/")
                        .httpMethod(HttpMethod.POST)
                        .extractPayload(true))
                .<String, String>transform(p -> p.substring(5))
                .transform(Transformers.fromJson(Atm[].class))
                .channel("responseChannel")
                .get();

      }

    }

The Gateway

package com.backbase.atm.service;

import java.util.List;

import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.messaging.handler.annotation.Payload;

import com.backbase.atm.model.Atm;

@MessagingGateway
public interface IntegrationService {

    @Gateway(requestChannel = "httpPostAtms")
    @Payload("new java.util.Date()")
    List<Atm> getAllAtms();
}

Application Start

package com.backbase.atm;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import com.backbase.atm.service.IntegrationService;

@SpringBootApplication
public class IngAtmApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(IngAtmApplication.class, args);
        ctx.getBean(IntegrationService.class).getAllAtms();
        ctx.close();
    }


问:
DataGridView How to Validate&Select cell
I have a grid view contains multiple columns and I adjusted their input type to be double.

When the user selects a cell, enters its data, I want to check if it matches the type of double.

If the user clicks on another cell and the one he is about to leave doesn't match and it failed in parsing, I want the focus return to the failed cell, and not moving to the new selected one, to force the user to enter a valid data before continuing filling the rest of the cells.

The problem is that it always leaves the failed cell. Even when I used other functions like validating or validated.

Here is my code sample:

```<code>private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {

            var selectedCell = dataGridView1.SelectedCells[0];
            if (selectedCell.OwningColumn.ValueType == typeof(double))
            {
                try
                {
                    double result = Convert.ToDouble(selectedCell.EditedFormattedValue.ToString());
                    if (result <= 0)
                    {
                        MessageBox.Show("Please Positive Numbers Only");
                        dataGridView1.Focus();
                        dataGridView1.CurrentCell = dataGridView1.Rows[selectedCell.RowIndex].Cells[selectedCell.ColumnIndex];
                        dataGridView1.BeginEdit(true);
                    }
                    else
                    {
                        dataGridView1.CurrentCell.Value = result;
                    }
                }
                catch
                {
                    MessageBox.Show("Please Enter Numbers Only");
                }
            }
        }
</code>```

答:
You can use event DataGridView.CellValidating. This should fix your problem.

Example:

```<code>   private void dataGridView1_CellValidating(object sender,
                                           DataGridViewCellValidatingEventArgs e)
    {
        if (e.ColumnIndex == 1) // 1 should be your column index
        {
            int i;

            if (!int.TryParse(Convert.ToString(e.FormattedValue), out i))
            {
                e.Cancel = true;
                label1.Text ="please enter numeric";
            }
            else
            {
                // the input is numeric
            }
        }
    }
</code>```

DataGridView.CellValidating Event

How to: Validate Data in the Windows Forms DataGridView Control

CellValueChanged vs CellValidating Events for DataGridView

Body:
I have a grid view contains multiple columns and I adjusted their input type to be double.

When the user selects a cell, enters its data, I want to check if it matches the type of double.

If the user clicks on another cell and the one he is about to leave doesn't match and it failed in parsing, I want the focus return to the failed cell, and not moving to the new selected one, to force the user to enter a valid data before continuing filling the rest of the cells.

The problem is that it always leaves the failed cell. Even when I used other functions like validating or validated.

Here is my code sample:

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {

            var selectedCell = dataGridView1.SelectedCells[0];
            if (selectedCell.OwningColumn.ValueType == typeof(double))
            {
                try
                {
                    double result = Convert.ToDouble(selectedCell.EditedFormattedValue.ToString());
                    if (result <= 0)
                    {
                        MessageBox.Show("Please Positive Numbers Only");
                        dataGridView1.Focus();
                        dataGridView1.CurrentCell = dataGridView1.Rows[selectedCell.RowIndex].Cells[selectedCell.ColumnIndex];
                        dataGridView1.BeginEdit(true);
                    }
                    else
                    {
                        dataGridView1.CurrentCell.Value = result;
                    }
                }
                catch
                {
                    MessageBox.Show("Please Enter Numbers Only");
                }
            }
        }

Body:
You can use event DataGridView.CellValidating. This should fix your problem.

Example:

   private void dataGridView1_CellValidating(object sender,
                                           DataGridViewCellValidatingEventArgs e)
    {
        if (e.ColumnIndex == 1) // 1 should be your column index
        {
            int i;

            if (!int.TryParse(Convert.ToString(e.FormattedValue), out i))
            {
                e.Cancel = true;
                label1.Text ="please enter numeric";
            }
            else
            {
                // the input is numeric
            }
        }
    }

DataGridView.CellValidating Event

How to: Validate Data in the Windows Forms DataGridView Control

CellValueChanged vs CellValidating Events for DataGridView


问:
SPA - handling DTOs
I want to implement SPA based on any js framework in e.g. Angular JS. I already have many existing Web APIs with informations that i need to show on app.I have to add 1 more that will manage new informations.New one will reference to other Dtos. My question is, better approach is to call APIs and compile Dto on web app or call APIs in new API merge all data and create GET method with ready object.

1st approach from website perspective:

```<code>Call (i know userId - based on logged in user [in this example = 1])

/get/order/{userId}
{
UserId: 1
Products:{1,2}
}

based on that call:

/get/user/1
{
FirstName: test,
LastName: test
}

/get/product/1
{
Price: 10,
Name: Test
}

/get/product/2
{
Price: 100,
Name: Test2
}
</code>```

merge all data in Angular Js Web

2nd approach from website perspective:

Merge everything on Order Web API

```<code>Call:

/get/ordermerged/{userId}
{
   FirstName: test,
   LastName: test
   Products:
   {
      {
         Price: 10,
         Name: Test
      },
      {
         Price: 100,
         Name: Test2
      }
   }
}
</code>```

I attach screenshot to better overview.

Diagram

答:
It's a trade-off between reusability and specialization. With reusability comes the obvious advantage - you may end up having to do less work. With specialization comes the advantage that your front end can consume the API as is. With specialization you'll also only ever load the data you actually need, whereas with many separated API's there might be redundant data and perhaps slightly more overhead.

I don't really think there's a notion of one approach being better than the other. It all depends on whether it's worth it for your specific situation. I'd argue that you should pick an approach per case. If some endpoints aren't really being reused anyway, you might as well create a "merged"/specialized version, where you get everything in one call. In other parts of your application, you may have endpoints that are more reusable and so you stick with them, and do a bit more "processing" client side.

Regarding performance (a single API call vs. multiple) the HTTP/2 protocol might give a small nudge in favor of multiple smaller API endpoints.

Whether one approach is better than the other for Angular specifically, doesn't really matter. Angular doesn't care. :-)

Title: SPA - handling DTOs

Body:
I want to implement SPA based on any js framework in e.g. Angular JS. I already have many existing Web APIs with informations that i need to show on app.I have to add 1 more that will manage new informations.New one will reference to other Dtos. My question is, better approach is to call APIs and compile Dto on web app or call APIs in new API merge all data and create GET method with ready object.

1st approach from website perspective:

Call (i know userId - based on logged in user [in this example = 1])

/get/order/{userId}
{
UserId: 1
Products:{1,2}
}

based on that call:

/get/user/1
{
FirstName: test,
LastName: test
}

/get/product/1
{
Price: 10,
Name: Test
}

/get/product/2
{
Price: 100,
Name: Test2
}

merge all data in Angular Js Web

2nd approach from website perspective:

Merge everything on Order Web API

Call:

/get/ordermerged/{userId}
{
   FirstName: test,
   LastName: test
   Products:
   {
      {
         Price: 10,
         Name: Test
      },
      {
         Price: 100,
         Name: Test2
      }
   }
}

I attach screenshot to better overview.

Diagram


问:
fatal: could not read Username for, No such device
In my Jenkins job, I am configuring git in the following way:

```<code>sh("git config user.email my.email@google.com")
sh("git config user.name my-user-name")
sh("git tag ${BUILD_NUMBER}")
sh("git push origin --tags")
</code>```

However, on the last line, when I try to push to my repository, I get the following error about the repository:

```<code>fatal: could not read Username for 'http://my-git-repo.com:8000': No such device or address
</code>```

What's wrong and how can I make it push to the repository?

答:
You're looking for credential.username. I don't know about the passphrase, but if you dislike http://username:password@my-git-repo.com:8000 then put the credentials in your Jenkinsfile like so:

The second most common type of Credentials is "Username and Password"
  which can still be used in the environment directive, but results in
  slightly different variables being set.

```<code>environment {
   SAUCE_ACCESS = credentials('sauce-lab-dev')
}
</code>```

This will actually set 3 environment variables:

```<code>SAUCE_ACCESS containing <username>:<password>
SAUCE_ACCESS_USR containing the username
SAUCE_ACCESS_PSW containing the password
</code>```

credentials is only available for Declarative Pipeline. For those
  using Scripted Pipeline, see the documentation for the
  withCredentials step.

Body:
In my Jenkins job, I am configuring git in the following way:

sh("git config user.email my.email@google.com")
sh("git config user.name my-user-name")
sh("git tag ${BUILD_NUMBER}")
sh("git push origin --tags")

However, on the last line, when I try to push to my repository, I get the following error about the repository:

fatal: could not read Username for 'http://my-git-repo.com:8000': No such device or address

What's wrong and how can I make it push to the repository?

Body:
You're looking for credential.username. I don't know about the passphrase, but if you dislike http://username:password@my-git-repo.com:8000 then put the credentials in your Jenkinsfile like so:

The second most common type of Credentials is "Username and Password"
  which can still be used in the environment directive, but results in
  slightly different variables being set.

environment {
   SAUCE_ACCESS = credentials('sauce-lab-dev')
}

This will actually set 3 environment variables:

SAUCE_ACCESS containing <username>:<password>
SAUCE_ACCESS_USR containing the username
SAUCE_ACCESS_PSW containing the password

credentials is only available for Declarative Pipeline. For those
  using Scripted Pipeline, see the documentation for the
  withCredentials step.


问:
Rendering react component after api response
I have a react component that I wish to populate with images using the Dropbox api. The api part works fine, but the component is rendered before the data comes through & so the array is empty. How can I delay the rendering of the component until it has the data it needs?

```<code>var fileList = [];
var images = [];
var imageSource = [];

class Foo extends React.Component {

 render(){
  dbx.filesListFolder({path: ''})
  .then(function(response) {
   fileList=response.entries;
   for(var i=0; i<fileList.length; i++){
    imageSource.push(fileList[0].path_lower);
   }
   console.log(imageSource);
   })

  for(var a=0; a<imageSource.length; a++){
   images.push(<img key={a} className='images'/>);
  }

  return (
   <div className="folioWrapper">
    {images}
   </div>
  );
 }
}
</code>```

Thanks for your help!

答:
You should be using your component's state or props so that it will re-render when data is updated. The call to Dropbox should be done outside of the render method or else you'll be hitting the API every time the component re-renders. Here's an example of what you could do.

```<code>class Foo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      imageSource: []
    }
  }

  componentDidMount() {
    dbx.filesListFolder({ path: '' }).then(function(response) {
      const fileList = response.entries;

      this.setState({
        imageSource: fileList.map(file => file.path_lower);
      })
    });
  }

  render() {
    return (
      <div className="folioWrapper">
        {this.state.imageSource.map((image, i) => <img key={i} className="images" src={image} />)}
      </div>
    );
  }
}
</code>```

If there are no images yet, it'll just render an empty div this way.

Body:
I have a react component that I wish to populate with images using the Dropbox api. The api part works fine, but the component is rendered before the data comes through & so the array is empty. How can I delay the rendering of the component until it has the data it needs?

var fileList = [];
var images = [];
var imageSource = [];

class Foo extends React.Component {

 render(){
  dbx.filesListFolder({path: ''})
  .then(function(response) {
   fileList=response.entries;
   for(var i=0; i<fileList.length; i++){
    imageSource.push(fileList[0].path_lower);
   }
   console.log(imageSource);
   })

  for(var a=0; a<imageSource.length; a++){
   images.push();
  }

  return (

    {images}

  );
 }
}

Thanks for your help!

Body:
You should be using your component's state or props so that it will re-render when data is updated. The call to Dropbox should be done outside of the render method or else you'll be hitting the API every time the component re-renders. Here's an example of what you could do.

class Foo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      imageSource: []
    }
  }

  componentDidMount() {
    dbx.filesListFolder({ path: '' }).then(function(response) {
      const fileList = response.entries;

      this.setState({
        imageSource: fileList.map(file => file.path_lower);
      })
    });
  }

  render() {
    return (
      <div className="folioWrapper">
        {this.state.imageSource.map((image, i) => <img key={i} className="images" src={image} />)}
      </div>
    );
  }
}

If there are no images yet, it'll just render an empty div this way.


问:
sharepoint 2010 central administration not starting after install
After installing sharepoint server 2010 on server 2008 64 bit . every thing goes well no errors,but after clicking sharepoint server 2010 central administration from the program menu i got this message

The version of this file compatible with the version of windows you you're running .Check yourf computer system information to see whether u need an x86 or x65 version of the program , and then contact the software publisher

browser issue? what could be wrong?

答:
Try to launch a few times the configuration wizard in sql server and in your SP server and it will come back.

It worked for me

ContentLicense: CC BY-SA 2.5


答:
Changes:

1. Don't do the api call inside render method, use componentDidMount lifecycle method for that.

componentDidMount:

componentDidMount() is invoked immediately after a component is
  mounted. Initialization that requires DOM nodes should go here. If you
  need to load data from a remote endpoint, this is a good place to
  instantiate the network request. Setting state in this method will
  trigger a re-rendering.

2. Define the imageSource variable in state array with initial value [], once you get the response update that using setState, it will automatically re-render the component with updated data.

3. Use the state array to generate the ui components in render method.

4. To hold the rendering until you didn't get the data, put the condition inside render method check the length of imageSource array if length is zero then return null.

Write it like this:

```<code>class Foo extends React.Component {

    constructor(){
        super();
        this.state = {
            imageSource: []
        }
    }

    componentDidMount(){
        dbx.filesListFolder({path: ''})
          .then((response) => {
              let fileList = response.entries;
              this.setState({
                  imageSource: fileList
              });
          })
    }

    render(){
        if(!this.state.imageSource.length)
            return null;

        let images = this.state.imageSource.map((el, i) => (
            <img key={i} className='images' src={el.path_lower} />
        ))

        return (
            <div className="folioWrapper">
                {images}
            </div>
        );
    }
}
</code>```

Title: Rendering react component after api response

Body:
Changes:

1. Don't do the api call inside render method, use componentDidMount lifecycle method for that.

componentDidMount:

componentDidMount() is invoked immediately after a component is
  mounted. Initialization that requires DOM nodes should go here. If you
  need to load data from a remote endpoint, this is a good place to
  instantiate the network request. Setting state in this method will
  trigger a re-rendering.

2. Define the imageSource variable in state array with initial value [], once you get the response update that using setState, it will automatically re-render the component with updated data.

3. Use the state array to generate the ui components in render method.

4. To hold the rendering until you didn't get the data, put the condition inside render method check the length of imageSource array if length is zero then return null.

Write it like this:

class Foo extends React.Component {

    constructor(){
        super();
        this.state = {
            imageSource: []
        }
    }

    componentDidMount(){
        dbx.filesListFolder({path: ''})
          .then((response) => {
              let fileList = response.entries;
              this.setState({
                  imageSource: fileList
              });
          })
    }

    render(){
        if(!this.state.imageSource.length)
            return null;

        let images = this.state.imageSource.map((el, i) => (
            <img key={i} className='images' src={el.path_lower} />
        ))

        return (
            <div className="folioWrapper">
                {images}
            </div>
        );
    }
}

ContentLicense: CC BY-SA 4.0


问:
Cassandra SELECT DISTINCT and timeout issue
When running the following CQL query:

```<code>SELECT DISTINCT partition_key FROM table_name;
</code>```

This is supposedly meant to return the list of partition keys that are in use for the given table. However, with the default timeout settings of 10s, it always times out:

```<code>ReadTimeout: Error from server: code=1200 [Coordinator node timed out waiting for replica nodes' responses] message="Operation timed out - received only 0 responses." info={'received_responses': 0, 'required_responses': 1, 'consistency': 'ONE'}
</code>```

Changing the timeout settings to:

```<code>read_request_timeout_in_ms: 60000
range_request_timeout_in_ms: 60000
request_timeout_in_ms: 60000
</code>```

And then running said query results in several Cassandra nodes crashing, including the coordinator node. The table has approximately >100M rows with about 5000 unique partition keys.

Is there a workaround to find the unique list of partition keys?

答:
There is another way of getting the list of keys using either of the following utilities:

```<code>sstabledump -e
     OR
$ bin/sstablekeys <sstable_name>
</code>```

But you need to run them across all nodes data directory and manually filter for distinct keys. Not straightforward but doable!

Here is the reference for the utilities Cassandra SSTabledump   and Cassandra SSTablekeys

The reason for query timeout is

No where clause in the query

Too many rows to scan through > 100M

Coordinator now has to keep the query open till gets responses from every node in the cluster and then filter for distinct.

The distinct operation is simply too costly for this usecase.

The nodes crash because essentially they fill up the heap with the entire rows being selected and cause OutOfMemory (OOM errors)

Body:
When running the following CQL query:

SELECT DISTINCT partition_key FROM table_name;

This is supposedly meant to return the list of partition keys that are in use for the given table. However, with the default timeout settings of 10s, it always times out:

ReadTimeout: Error from server: code=1200 [Coordinator node timed out waiting for replica nodes' responses] message="Operation timed out - received only 0 responses." info={'received_responses': 0, 'required_responses': 1, 'consistency': 'ONE'}

Changing the timeout settings to:

read_request_timeout_in_ms: 60000
range_request_timeout_in_ms: 60000
request_timeout_in_ms: 60000

And then running said query results in several Cassandra nodes crashing, including the coordinator node. The table has approximately >100M rows with about 5000 unique partition keys.

Is there a workaround to find the unique list of partition keys?

Body:
There is another way of getting the list of keys using either of the following utilities:

sstabledump -e
     OR
$ bin/sstablekeys <sstable_name>

But you need to run them across all nodes data directory and manually filter for distinct keys. Not straightforward but doable!

Here is the reference for the utilities Cassandra SSTabledump   and Cassandra SSTablekeys

The reason for query timeout is

No where clause in the query

Too many rows to scan through > 100M

Coordinator now has to keep the query open till gets responses from every node in the cluster and then filter for distinct.

The distinct operation is simply too costly for this usecase.

The nodes crash because essentially they fill up the heap with the entire rows being selected and cause OutOfMemory (OOM errors)


问:
Send data to server side from client
I'm currently creating a simple web-based data visualization tool. I basically want to be able to send the user selections to the server (which I have created using express) to query the database. The problem is I am not sure how to send the data to the server and then receive it at the other end in order to do this.

I want the data to be sent to the server on the click of a button

var data = {
    Admission_type: "";
    Gender: "";
    Max_age: 89,
    Min_age: 0
};

$( "#PlotBtn" ).click(function() {

});```

答:
The most common vanilla JS way of sending data to a server is with to make a XMLHttpRequest (XHR). It seems like you're trying to pass an object, and JSON is perhaps the most flexible way of sending data to and from a server. So the simplest piece of code that would send data to the server is something like this:

```<code>var xhr = new XMLHttpRequest;
xhr.open("GET", "https://yourserver.domain.com/data");
xhr.send();
</code>```

This however will only send the information and it has no way of handling the response. There are many ways to go about this, as well as many options on how the information is sent, but this is the barebones you need to get the response.

```<code>xhr.onload = function(){
  console.log(xhr.response);
};
</code>```

Take a look at the documentation linked above to see more options and other events you can listen to.

Title: Send data to server side from client

Body:
The most common vanilla JS way of sending data to a server is with to make a XMLHttpRequest (XHR). It seems like you're trying to pass an object, and JSON is perhaps the most flexible way of sending data to and from a server. So the simplest piece of code that would send data to the server is something like this:

var xhr = new XMLHttpRequest;
xhr.open("GET", "https://yourserver.domain.com/data");
xhr.send();

This however will only send the information and it has no way of handling the response. There are many ways to go about this, as well as many options on how the information is sent, but this is the barebones you need to get the response.

xhr.onload = function(){
  console.log(xhr.response);
};

Take a look at the documentation linked above to see more options and other events you can listen to.


问:
Stanford CoreNLP BasicPipelineExample doesn't work
I'm trying to get started with Stanford CoreNLP and can't even get past the very first simple example from here.

https://stanfordnlp.github.io/CoreNLP/api.html

Here is my code:

```<code>package stanford.corenlp;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.google.common.io.Files;

import edu.stanford.nlp.dcoref.CorefChain;
import edu.stanford.nlp.dcoref.CorefCoreAnnotations.CorefChainAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.NamedEntityTagAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation;
import edu.stanford.nlp.util.CoreMap;
import java.util.logging.Level;
import java.util.logging.Logger;

    private void test2() {
        // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        // read some text in the text variable
        String text = "Now is the time for all good men to come to the aid of their country.";

        // create an empty Annotation just with the given text
        Annotation document = new Annotation(text);

        // run all Annotators on this text
        pipeline.annotate(document);

    }

  public static void main(String[] args) throws IOException {
      StanfordNLP nlp = new StanfordNLP();
      nlp.test2();
  }

}
</code>```

Here is the stacktrace:

```<code>Adding annotator tokenize
No tokenizer type provided. Defaulting to PTBTokenizer.
Adding annotator ssplit
Adding annotator pos
Exception in thread "main" edu.stanford.nlp.io.RuntimeIOException: Error while loading a tagger model (probably missing model file)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:791)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:312)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:265)
    at edu.stanford.nlp.pipeline.POSTaggerAnnotator.loadModel(POSTaggerAnnotator.java:85)
    at edu.stanford.nlp.pipeline.POSTaggerAnnotator.<init>(POSTaggerAnnotator.java:73)
    at edu.stanford.nlp.pipeline.AnnotatorImplementations.posTagger(AnnotatorImplementations.java:55)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getNamedAnnotators$42(StanfordCoreNLP.java:496)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getDefaultAnnotatorPool$65(StanfordCoreNLP.java:533)
    at edu.stanford.nlp.util.Lazy$3.compute(Lazy.java:118)
    at edu.stanford.nlp.util.Lazy.get(Lazy.java:31)
    at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:146)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:447)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:150)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:146)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:133)
    at stanford.corenlp.StanfordNLP.test2(StanfordNLP.java:93)
    at stanford.corenlp.StanfordNLP.main(StanfordNLP.java:108)
Caused by: java.io.IOException: Unable to open "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger" as class path, filename or URL
    at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:480)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:789)
    ... 16 more
C:\Users\Greg\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
</code>```

What am I missing?

答:
First of all you need to add to the class path stanford-corenlp-3.8.0.jar.  That makes the red error marks in NetBeans go away.  But you also need to add stanford-corenlp-3.8.0-models.jar to the class path to prevent the error I documented.  Adding the folder it resides in to the classpath doesn't work.  Details like this should never be left out of beginner documentation!

Now if you continue with the example and add in the new stuff, more errors occur.  For example, the code will then look like:

```<code>package stanford.corenlp;

    import java.io.File;
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;

    import com.google.common.io.Files;

    import edu.stanford.nlp.dcoref.CorefChain;
    import edu.stanford.nlp.dcoref.CorefCoreAnnotations.CorefChainAnnotation;
    import edu.stanford.nlp.ling.CoreAnnotations.NamedEntityTagAnnotation;
    import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation;
    import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
    import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation;
    import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
    import edu.stanford.nlp.ling.CoreLabel;
    import edu.stanford.nlp.pipeline.Annotation;
    import edu.stanford.nlp.pipeline.StanfordCoreNLP;
    import edu.stanford.nlp.semgraph.SemanticGraph;
    import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation;
    import edu.stanford.nlp.trees.Tree;
    import edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation;
    import edu.stanford.nlp.util.CoreMap;
    import edu.stanford.nlp.util.PropertiesUtils;
    import java.util.logging.Level;
    import java.util.logging.Logger;

        private void test2() {
            // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution
            Properties props = new Properties();
            props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(
            PropertiesUtils.asProperties(
                "annotators", "tokenize,ssplit,pos,lemma,parse,natlog",
                "ssplit.isOneSentence", "true",
                "parse.model", "edu/stanford/nlp/models/srparser/englishSR.ser.gz",
                "tokenize.language", "en"));

            // read some text in the text variable
            String text = "Now is the time for all good men to come to the aid of their country.";

            // create an empty Annotation just with the given text
            Annotation document = new Annotation(text);

            // run all Annotators on this text
            pipeline.annotate(document);

            // these are all the sentences in this document
            // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
            List<CoreMap> sentences = document.get(SentencesAnnotation.class);

            for (CoreMap sentence: sentences) {
                // traversing the words in the current sentence
                // a CoreLabel is a CoreMap with additional token-specific methods
                for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
                    // this is the text of the token
                    String word = token.get(TextAnnotation.class);
                    // this is the POS tag of the token
                    String pos = token.get(PartOfSpeechAnnotation.class);
                    // this is the NER label of the token
                    String ne = token.get(NamedEntityTagAnnotation.class);

                    System.out.println("word="+word +", pos="+pos +", ne="+ne);
                }

                // this is the parse tree of the current sentence
                Tree tree = sentence.get(TreeAnnotation.class);

                // this is the Stanford dependency graph of the current sentence
                SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
            }

            // This is the coreference link graph
            // Each chain stores a set of mentions that link to each other,
            // along with a method for getting the most representative mention
            // Both sentence and token offsets start at 1!
            Map<Integer, CorefChain> graph =
                document.get(CorefChainAnnotation.class);
        }

      public static void main(String[] args) throws IOException {
          StanfordNLP nlp = new StanfordNLP();
          nlp.test2();
      }

    }
</code>```

And the stack trace becomes:

```<code>run:
Adding annotator tokenize
Adding annotator ssplit
Adding annotator pos
Loading POS tagger from edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger ... done [0.6 sec].
Adding annotator lemma
Adding annotator parse
Exception in thread "main" edu.stanford.nlp.io.RuntimeIOException: java.io.IOException: Unable to open "edu/stanford/nlp/models/srparser/englishSR.ser.gz" as class path, filename or URL
    at edu.stanford.nlp.parser.common.ParserGrammar.loadModel(ParserGrammar.java:187)
    at edu.stanford.nlp.pipeline.ParserAnnotator.loadModel(ParserAnnotator.java:219)
    at edu.stanford.nlp.pipeline.ParserAnnotator.<init>(ParserAnnotator.java:121)
    at edu.stanford.nlp.pipeline.AnnotatorImplementations.parse(AnnotatorImplementations.java:115)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getNamedAnnotators$50(StanfordCoreNLP.java:504)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getDefaultAnnotatorPool$65(StanfordCoreNLP.java:533)
    at edu.stanford.nlp.util.Lazy$3.compute(Lazy.java:118)
    at edu.stanford.nlp.util.Lazy.get(Lazy.java:31)
    at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:146)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:447)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:150)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:146)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:133)
    at stanford.corenlp.StanfordNLP.test2(StanfordNLP.java:95)
    at stanford.corenlp.StanfordNLP.main(StanfordNLP.java:145)
Caused by: java.io.IOException: Unable to open "edu/stanford/nlp/models/srparser/englishSR.ser.gz" as class path, filename or URL
    at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:480)
    at edu.stanford.nlp.io.IOUtils.readObjectFromURLOrClasspathOrFileSystem(IOUtils.java:309)
    at edu.stanford.nlp.parser.common.ParserGrammar.loadModel(ParserGrammar.java:184)
    ... 14 more
C:\Users\Greg\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
</code>```

I finally got past this point by downloading and adding to the classpath stanford-english-corenlp-2017-06-09-models.jar which you can get from the "English" download link here:

https://stanfordnlp.github.io/CoreNLP/download.html

You need to do this despite the message on the Download page saying that everything needed for English is already provided in the corenlp download!

Body:
I'm trying to get started with Stanford CoreNLP and can't even get past the very first simple example from here.

https://stanfordnlp.github.io/CoreNLP/api.html

Here is my code:

package stanford.corenlp;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.google.common.io.Files;

import edu.stanford.nlp.dcoref.CorefChain;
import edu.stanford.nlp.dcoref.CorefCoreAnnotations.CorefChainAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.NamedEntityTagAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation;
import edu.stanford.nlp.util.CoreMap;
import java.util.logging.Level;
import java.util.logging.Logger;

    private void test2() {
        // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        // read some text in the text variable
        String text = "Now is the time for all good men to come to the aid of their country.";

        // create an empty Annotation just with the given text
        Annotation document = new Annotation(text);

        // run all Annotators on this text
        pipeline.annotate(document);

    }

  public static void main(String[] args) throws IOException {
      StanfordNLP nlp = new StanfordNLP();
      nlp.test2();
  }

}

Here is the stacktrace:

Adding annotator tokenize
No tokenizer type provided. Defaulting to PTBTokenizer.
Adding annotator ssplit
Adding annotator pos
Exception in thread "main" edu.stanford.nlp.io.RuntimeIOException: Error while loading a tagger model (probably missing model file)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:791)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:312)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:265)
    at edu.stanford.nlp.pipeline.POSTaggerAnnotator.loadModel(POSTaggerAnnotator.java:85)
    at edu.stanford.nlp.pipeline.POSTaggerAnnotator.<init>(POSTaggerAnnotator.java:73)
    at edu.stanford.nlp.pipeline.AnnotatorImplementations.posTagger(AnnotatorImplementations.java:55)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getNamedAnnotators$42(StanfordCoreNLP.java:496)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getDefaultAnnotatorPool$65(StanfordCoreNLP.java:533)
    at edu.stanford.nlp.util.Lazy$3.compute(Lazy.java:118)
    at edu.stanford.nlp.util.Lazy.get(Lazy.java:31)
    at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:146)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:447)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:150)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:146)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:133)
    at stanford.corenlp.StanfordNLP.test2(StanfordNLP.java:93)
    at stanford.corenlp.StanfordNLP.main(StanfordNLP.java:108)
Caused by: java.io.IOException: Unable to open "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger" as class path, filename or URL
    at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:480)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:789)
    ... 16 more
C:\Users\Greg\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

What am I missing?

Body:
First of all you need to add to the class path stanford-corenlp-3.8.0.jar.  That makes the red error marks in NetBeans go away.  But you also need to add stanford-corenlp-3.8.0-models.jar to the class path to prevent the error I documented.  Adding the folder it resides in to the classpath doesn't work.  Details like this should never be left out of beginner documentation!

Now if you continue with the example and add in the new stuff, more errors occur.  For example, the code will then look like:

package stanford.corenlp;

    import java.io.File;
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;

    import com.google.common.io.Files;

    import edu.stanford.nlp.dcoref.CorefChain;
    import edu.stanford.nlp.dcoref.CorefCoreAnnotations.CorefChainAnnotation;
    import edu.stanford.nlp.ling.CoreAnnotations.NamedEntityTagAnnotation;
    import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation;
    import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
    import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation;
    import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
    import edu.stanford.nlp.ling.CoreLabel;
    import edu.stanford.nlp.pipeline.Annotation;
    import edu.stanford.nlp.pipeline.StanfordCoreNLP;
    import edu.stanford.nlp.semgraph.SemanticGraph;
    import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation;
    import edu.stanford.nlp.trees.Tree;
    import edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation;
    import edu.stanford.nlp.util.CoreMap;
    import edu.stanford.nlp.util.PropertiesUtils;
    import java.util.logging.Level;
    import java.util.logging.Logger;

        private void test2() {
            // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution
            Properties props = new Properties();
            props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(
            PropertiesUtils.asProperties(
                "annotators", "tokenize,ssplit,pos,lemma,parse,natlog",
                "ssplit.isOneSentence", "true",
                "parse.model", "edu/stanford/nlp/models/srparser/englishSR.ser.gz",
                "tokenize.language", "en"));

            // read some text in the text variable
            String text = "Now is the time for all good men to come to the aid of their country.";

            // create an empty Annotation just with the given text
            Annotation document = new Annotation(text);

            // run all Annotators on this text
            pipeline.annotate(document);

            // these are all the sentences in this document
            // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
            List<CoreMap> sentences = document.get(SentencesAnnotation.class);

            for (CoreMap sentence: sentences) {
                // traversing the words in the current sentence
                // a CoreLabel is a CoreMap with additional token-specific methods
                for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
                    // this is the text of the token
                    String word = token.get(TextAnnotation.class);
                    // this is the POS tag of the token
                    String pos = token.get(PartOfSpeechAnnotation.class);
                    // this is the NER label of the token
                    String ne = token.get(NamedEntityTagAnnotation.class);

                    System.out.println("word="+word +", pos="+pos +", ne="+ne);
                }

                // this is the parse tree of the current sentence
                Tree tree = sentence.get(TreeAnnotation.class);

                // this is the Stanford dependency graph of the current sentence
                SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
            }

            // This is the coreference link graph
            // Each chain stores a set of mentions that link to each other,
            // along with a method for getting the most representative mention
            // Both sentence and token offsets start at 1!
            Map<Integer, CorefChain> graph =
                document.get(CorefChainAnnotation.class);
        }

      public static void main(String[] args) throws IOException {
          StanfordNLP nlp = new StanfordNLP();
          nlp.test2();
      }

    }

And the stack trace becomes:

run:
Adding annotator tokenize
Adding annotator ssplit
Adding annotator pos
Loading POS tagger from edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger ... done [0.6 sec].
Adding annotator lemma
Adding annotator parse
Exception in thread "main" edu.stanford.nlp.io.RuntimeIOException: java.io.IOException: Unable to open "edu/stanford/nlp/models/srparser/englishSR.ser.gz" as class path, filename or URL
    at edu.stanford.nlp.parser.common.ParserGrammar.loadModel(ParserGrammar.java:187)
    at edu.stanford.nlp.pipeline.ParserAnnotator.loadModel(ParserAnnotator.java:219)
    at edu.stanford.nlp.pipeline.ParserAnnotator.<init>(ParserAnnotator.java:121)
    at edu.stanford.nlp.pipeline.AnnotatorImplementations.parse(AnnotatorImplementations.java:115)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getNamedAnnotators$50(StanfordCoreNLP.java:504)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getDefaultAnnotatorPool$65(StanfordCoreNLP.java:533)
    at edu.stanford.nlp.util.Lazy$3.compute(Lazy.java:118)
    at edu.stanford.nlp.util.Lazy.get(Lazy.java:31)
    at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:146)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:447)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:150)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:146)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:133)
    at stanford.corenlp.StanfordNLP.test2(StanfordNLP.java:95)
    at stanford.corenlp.StanfordNLP.main(StanfordNLP.java:145)
Caused by: java.io.IOException: Unable to open "edu/stanford/nlp/models/srparser/englishSR.ser.gz" as class path, filename or URL
    at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:480)
    at edu.stanford.nlp.io.IOUtils.readObjectFromURLOrClasspathOrFileSystem(IOUtils.java:309)
    at edu.stanford.nlp.parser.common.ParserGrammar.loadModel(ParserGrammar.java:184)
    ... 14 more
C:\Users\Greg\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

I finally got past this point by downloading and adding to the classpath stanford-english-corenlp-2017-06-09-models.jar which you can get from the "English" download link here:

https://stanfordnlp.github.io/CoreNLP/download.html

You need to do this despite the message on the Download page saying that everything needed for English is already provided in the corenlp download!


问:
mean squared error in scikit learn RidgeCV
My question is: In sklearn, how is the cv_values_ given by RidgeCV calculated? why is it different with output from metrics.mean_squared_error?

For example,

```<code>X = [1,2,3,4,5,6,7,8,9,10]
X = np.array(X).reshape(-1,1)
y = np.array([1,3.5,4,4.9,6.1,7.2,8.1,8.9,10,11.1])
ax.plot(X, y, 'o')
ax.plot(X, X+1, '-') # help visualize
</code>```

Say we train the Ridge model on X and y

```<code>from sklearn.linear_model import RidgeCV
from sklearn.metrics import mean_squared_error
model = RidgeCV(alphas = [0.001], store_cv_values=True).fit(X, y)
</code>```

Now the output of

```<code>mean_squared_error(y_true=y, y_pred=model.predict(X))
</code>```

is 0.1204000013110009, while the output of

```<code>model.cv_values_.mean()
</code>```

is 0.24472577167818438.

Why is there such a huge difference? Am I missing something obvious?

答:
From the official website link

cv_values_

Cross-validation values for each alpha (if store_cv_values=True and cv=None). After fit() has been called, this attribute will contain the mean squared errors (by default) or the values of the {loss,score}_func function (if provided in the constructor).

In your case when you call the

model = RidgeCV(alphas = [0.001], store_cv_values=True).fit(X, y)

you have: cv=None

cv=None means that you use the Leave-One-Out cross-validation.

So cv_values stores the mean squared error for each sample using Leave-One out cross validation. In every fold you have only 1 test point and thus n = 1. So cv_values_ will give you the squared error for every point in your training data set when it was a part of the test fold.

Finally, this means that when you call model.cv_values_.mean(), you get the mean of these individual errors (mean of each error for each point). To see these individual error you can use print(model.cv_values_)

Individual means that the n=1 in the following equation:

On the other hand, mean_squared_error(y_true=y, y_pred=model.predict(X)) means that you put n=10 in this equation.

So the 2 results will differ.

Body:
My question is: In sklearn, how is the cv_values_ given by RidgeCV calculated? why is it different with output from metrics.mean_squared_error?

For example,

X = [1,2,3,4,5,6,7,8,9,10]
X = np.array(X).reshape(-1,1)
y = np.array([1,3.5,4,4.9,6.1,7.2,8.1,8.9,10,11.1])
ax.plot(X, y, 'o')
ax.plot(X, X+1, '-') # help visualize

Say we train the Ridge model on X and y

from sklearn.linear_model import RidgeCV
from sklearn.metrics import mean_squared_error
model = RidgeCV(alphas = [0.001], store_cv_values=True).fit(X, y)

Now the output of

mean_squared_error(y_true=y, y_pred=model.predict(X))

is 0.1204000013110009, while the output of

model.cv_values_.mean()

is 0.24472577167818438.

Why is there such a huge difference? Am I missing something obvious?